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?
derobert13 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. WilsonYes, if you don't mind the extra newlines being inserted:
Alternatively you can use a heredoc:
smile2dayThere 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.
HongliHongliSometimes is worth to remove new line characters n
like:
You can also use double quotes
If needed to remove line breaks 'n' use backslash ' at the end of each line
Ruby Concatenate String And Variable
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
If you do mind extra spaces and newlines, you can use
(use %W for interpolated strings)
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
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: