I was trying to write a single string across multiple lines since it was too long and I arrived at this solution that I think looks best but I couldn't find anything about it in the Ruby documentation
my_original = 'what I originally had ' +
'across multiple lines'
# executes to: "what I originally had across multiple lines"
new_style = 'new format to '\
'span multiple lines'
# executes to: "new format to span multiple lines\n"
However I saw of lot of ways this could be done and I was wondering whether they uses concatenation or interpolation and all I could find was this. In this case performance is not particularly important in this case but having the knowledge of what goes on under the covers cant hurt. So was wondering the differences between these.
my_original = 'what I originally had ' +
'across multiple lines'
# executes to: "what I originally had across multiple lines"
s_one = 'I assume this is a more '
s_two = 'verbose version of the '
s_three = 'first example.'
my_string = s_one + s_two + s_three
# executes to: "I assume this is a more verbose version of the first example."
my_first_solution = 'that breaks whenever'
my_first_solution << 'ruby 3.0 might be released'
# executes to:
style_i_used = 'which can span multiple '\
'lines without having '\
'extra white space'
# executes to: "which can span multiple lines without having extra white space"
another_string = <<-HEREDOC
when you don't mind
really wonky indentation
or having extra spaces.
HEREDOC
# executes to:"when you don't mind \nreally wonky indentation \n or having extra spaces\n and new lines. \n"
# "Is this just a one line string with no special characters?# executes to:
another_string = <<~HEREDOC
I assume this is the same as
the non squiggly version with
stripping between lines.
HEREDOC
# executes to: "I assume this is the same as \nthe non squiggly version with \nstripping between lines.\n"
#Edit
another_one =
"I did not originally include; however,
this one also adds a bunch of extra
white space and new lines."
# executes to:"I did not originally include; however,\n this one also adds a bunch of extra \n white space and new lines."
This is a NoMethodError
style_i_used = 'a format to' /
'span multiple lines'
/
is a method call which Strings don't have. \
on the other hand "escapes" the newline so it's treated as one string.
These won't give an error, but are bad:
my_original = 'what I originally had' +
'across multiple lines'
s_one = 'I assume this is the same as '
s_two = 'the first example with the '
s_three = 'code being more verbose'
my_string = s_one + s_two + s_three
my_first_solution = 'that breaks whenever'
my_first_solution << 'ruby 3.0 might be released'
+
and <<
are method calls. I don't think the Ruby parser is smart enough to see that these can all be compile-time strings so these strings are actually being constructed during run-time with object allocation and everything. That is entirely unnecessary.
The other kinds have different usage/results, so you use whichever is appropriate.
str = "a"\
"b" # result is "ab"
str = "a\
b" # also "ab"
str = "a
b" # result is "a\nb"
str = <<END
'"a'"
'"b'"
END
# result is " '\"a'\"\n '\"b'\""
str = <<~END
'"a'"
'"b'"
END
# result is "'\"a'\"\n'\"b'\""