I'm reviewing a line of Ruby code in a pull request. I'm not sure if this is a bug or a feature that I haven't seen before:
puts "A string of Ruby that"\
"continues on the next line"
Is the backslash a valid character to concatenate these strings? Or is this a bug?
That is valid code.
The backslash is a line continuation. Your code has two quoted runs of text; the runs appear like two strings, but are really just one string because Ruby concatenates whitespace-separated runs.
Example of three quoted runs of text that are really just one string:
"a" "b" "c"
=> "abc"
Example of three quoted runs of text that are really just one string, using \
line continuations:
"a" \
"b" \
"c"
=> "abc"
Example of three strings, using +
line continuations and also concatenations:
"a" +
"b" +
"c"
=> "abc"
Other line continuation details: "Ruby interprets semicolons and newline characters as the ending of a statement. However, if Ruby encounters operators, such as +, -, or backslash at the end of a line, they indicate the continuation of a statement." - Ruby Quick Guide
Edit: Good clarification from @cesoid in the comments... Backslash only indicates the continuation of a statement, whereas + and - continue the statement and perform their normal operation. In other words, the backslash gets "removed", but the + and - don't. It makes more sense to say that + and - are operators that work across multiple lines (as do other things in ruby). Backslashes are different. For example, putting backlslashes between two strings on one line causes a syntax error.