Peter Alfvin Peter Alfvin - 1 year ago 31
Ruby Question

Where is Ruby's string literal juxtaposition feature officially documented?

I recently realized that if you juxtapose a sequence of Ruby string literals (e.g.

'a' "b" 'c'
), it's equivalent to the concatenation of those string literals. However, I can't find this language feature documented anywhere. I've searched using the terms "juxtaposition" and "concatenation", but only found reference to it in a couple of StackOverflow responses. Can anyone point me to a definitive reference?

Answer Source


This is now officially documented in the RDoc that ships with Ruby.

Changes will propagate to RubyDoc the next time they build the documentation.

The added documentation:

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 single-quote, double-quote, percent strings will
be concatenated as long as a percent-string is not last.

  %q{a} 'b' "c" #=> "abc"
  "a" 'b' %q{c} #=> NameError: uninitialized constant q


Right now, this isn't anywhere in the official ruby documentation, but I think it should be. As pointed out in a comment, the logical place for the docs to go would be:

I've opened a pull request on ruby/ruby with the documentation added.

If this pull request is merged, it will automatically update I'll update this post if/when that happens. ^_^

The only other mentions of this I've found online are: