Ruby Append String

We can do this through ruby string concatenation or the joining of strings. We'll show you how to do string concatenation in our main program. In a bit, but first let's try it out in a different way. I want to show you a separate program that gets installed along with Ruby called irb. A String object holds and manipulates an arbitrary sequence of bytes, typically representing characters. String objects may be created using String::new or as literals. Because of aliasing issues, users of strings should be aware of the methods that modify the contents of a String object. Output as an Array. If you're appending multiple times to the same String variable and you want to later output each of these appended Strings in new lines, you might want to consider using an Array to store each String in and then just join them with a new line when displaying/outputting them. Inserting elements in a ruby array is very straight forward. We have 3 methods on how to insert elements on arrays. Let’s try them on our example. 2.0.0-p0:014 letters = Array.new('a', 'b', 'c') = 'a', 'b', 'c'. A string is a sequence of characters. Strings are objects so they have a lot of methods you can use to do things with them. In this article you'll discover the most useful Ruby string methods with.

IRB stands for 'Interactive RuBy'. You can run it by clicking in your console or terminal and typing irb at the system prompt. When we run it, it'll show you a prompt where you can type Ruby expressions one at a time, hitting Enter after each. IRB will immediately show you the result of each expression.

Active10 months ago

Is there a way to make this look a little better?

Ruby append string to array elements

Like, is there a way to imply concatenation?

derobert

Ruby Append List

41k6 gold badges79 silver badges116 bronze badges
ZombiesZombies
11.2k34 gold badges124 silver badges206 bronze badges

13 Answers

There are pieces to this answer that helped me get what I needed (easy multi-line concatenation WITHOUT extra whitespace), but since none of the actual answers had it, I'm compiling them here:

As a bonus, here's a version using funny HEREDOC syntax (via this link):

The latter would mostly be for situations that required more flexibility in the processing. I personally don't like it, it puts the processing in a weird place w.r.t. the string (i.e., in front of it, but using instance methods that usually come afterward), but it's there. Note that if you are indenting the last END_SQL identifier (which is common, since this is probably inside a function or module), you will need to use the hyphenated syntax (that is, p <<-END_SQL instead of p <<END_SQL). Otherwise, the indenting whitespace causes the identifier to be interpreted as a continuation of the string.

This doesn't save much typing, but it looks nicer than using + signs, to me.

EDIT: Adding one more:

A. WilsonA. Wilson
6,5141 gold badge18 silver badges33 bronze badges

Yes, if you don't mind the extra newlines being inserted:

Alternatively you can use a heredoc:

smile2day
1,1811 gold badge17 silver badges26 bronze badges
Mark ByersMark Byers
627k139 gold badges1405 silver badges1360 bronze badges
Robbie GuilfoyleRobbie Guilfoyle
2,4981 gold badge13 silver badges17 bronze badges

There are multiple syntaxes for multi-line strings as you've already read. My favorite is Perl-style:

The multi-line string starts with %q, followed by a {, [ or (, and then terminated by the corresponding reversed character. %q does not allow interpolation; %Q does so you can write things like this:

I actually have no idea how these kinds of multi-line strings are called so let's just call them Perl multilines.

Note however that whether you use Perl multilines or heredocs as Mark and Peter have suggested, you'll end up with potentially unnecessary whitespaces. Both in my examples and their examples, the 'from' and 'where' lines contain leading whitespaces because of their indentation in the code. If this whitespace is not desired then you must use concatenated strings as you are doing now.

HongliRubyHongli
14.6k13 gold badges69 silver badges99 bronze badges

Sometimes is worth to remove new line characters n like:

Kamil LelonekKamil Lelonek
10.1k10 gold badges52 silver badges80 bronze badges

You can also use double quotes

If needed to remove line breaks 'n' use backslash ' at the end of each line

juliangonzalezjuliangonzalez
PeterPeter
88.4k41 gold badges163 silver badges202 bronze badges
Alex CohenAlex Cohen
1,5893 gold badges24 silver badges56 bronze badges

Recently with the new features in Ruby 2.3 the new squiggly HEREDOC will let you write our multiline strings in a nice manner with a minimal change so using this combined with the .squish will let you write multiline in a nice way!

ref: https://infinum.co/the-capsized-eight/multiline-strings-ruby-2-3-0-the-squiggly-heredoc

Mark JadMark Jad

Ruby Add To String

Dom BrezinskiDom Brezinski

If you do mind extra spaces and newlines, you can use

(use %W for interpolated strings)

UncleGeneUncleGene

This suggestion has the advantage over here-documents and long strings that auto-indenters can indent each part of the string appropriately. But it comes at an efficiency cost.

Aidan CullyAidan Cully

To avoid closing the parentheses for each line you can simply use double quotes with a backslash to escape the newline:

PwnrarPwnrar

Not the answer you're looking for? Browse other questions tagged rubycode-formatting or ask your own question.