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.
Is there a way to make this look a little better?
Like, is there a way to imply concatenation?
derobertRuby Append List
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. 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
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
Ruby Add To String
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.
To avoid closing the parentheses for each line you can simply use double quotes with a backslash to escape the newline: