Ruby silently concatenates adjacent string literals
Given this code,
a = [ "Hello" "world!" ]
p a.size
what will the output be?
Your immediate answer is likely to be "2". (Unless you paid attention to the title of this protip.) However, it will be "1". Go ahead and try it in irb. I'll wait.
The Ruby parser automatically concatenates adjacent string literals. They don't have to have the same kind of quote characters, and interpolation is still applied.
p "Hello " 'world!'
s = 'world'
p "Hello " "#{s}!"
This is documented behavior for the Ruby parser, which means it won't even generate a warning.
Written by Craig S. Cottingham
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Related Tags
#ruby
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#