Why are two lines separated by space combined in Ruby?
Why does this work in Ruby:
"foo" "bar" # => "foobar" I am not sure why the lines were concatenated instead of a syntax error.
I am curious whether this behavior should be expected or not, that the parser is responsible for disputes (two lines without operators are considered one line) or the language definition itself determines this behavior (implicit CONCAT).
Implementation details can be found in the parse.y file in the Ruby source code. In particular, here .
A Ruby string is either tCHAR (for example ?q ), a string1 (for example, "q", "q" or% q (q)), or a recursive definition of concatenation from string1 and string , which leads to concatenation of string expressions such like "foo" "bar" , 'foo' "bar" or ?f "oo" 'bar' .
In C and C ++, string literals are combined next to each other. Since these languages ββinfluenced Ruby, I would suggest that it inherits from there.
And now this is described in Ruby: see this answer and this page in the Ruby repo , which says:
Adjacent string literals are automatically concatenated by the interpreter:
"con" "cat" "en" "at" "ion" #=> "concatenation" "This string contains "\ "no newlines." #=> "This string contains no newlines."Any combination of adjacent lines with one quote, double quote, percent will be concatenated until the percentage line is the last.
%q{a} 'b' "c" #=> "abc" "a" 'b' %q{c} #=> NameError: uninitialized constant q