Ruby Append String

Active10 months ago

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. Jan 01, 2009  The first section of the chapter shows the most basic and common way to create an array, but there are alternatives (in Ruby, there are always alternatives). You can start by creating a new empty array by doing either Figure 4.8 Four elements are added to an array. The first two lines demonstrate.

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

Like, is there a way to imply concatenation?

derobert
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):

Append

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 ByersStringMark 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.

HongliHongliRuby
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

Ruby Concatenate String And Variable

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
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.

Ruby Append String File

Aidan CullyAidan Cully

Ruby Append String With Newline

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

Ruby Append String In Excel

PwnrarPwnrar

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