12

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?

Peter Alfvin
  • 28,599
  • 8
  • 68
  • 106
  • Well, it's *not* documented at http://www.ruby-doc.org/core-2.0/doc/syntax/literals_rdoc.html#label-Strings, which seems like the logical place for it . . . – ruakh Aug 12 '13 at 18:20
  • 1
    It's actually the Ruby parser that does this. It's an optimization and documented here: http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html – Henrik Andersson Aug 12 '13 at 18:23
  • 4
    Direct to the "Strings" section: http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html#UD – Dave Newton Aug 12 '13 at 18:27
  • @DaveNewton and others - Thanks! There it is, succinctly stated as-per-Ruby-usual: "Adjacent single- and double-quoted strings in the input are concatenated to form a single String object." – Peter Alfvin Aug 12 '13 at 18:48

4 Answers4

10

UPDATE

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

ORIGINAL

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: http://www.ruby-doc.org/core-2.0/doc/syntax/literals_rdoc.html#label-Strings

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

If this pull request is merged, it will automatically update http://www.ruby-doc.org. I'll update this post if/when that happens. ^_^

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

Chris Knadler
  • 2,819
  • 2
  • 26
  • 32
  • Thanks, Chris. I'd consider http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html, mentioned in the comments on the question, to be in the same category as the ones you've mentioned, though, wouldn't you? – Peter Alfvin Aug 12 '13 at 19:03
  • @PeterAlfvin The documentation is directly related to string literals so I think it would belong in the literals doc that ships with the language. The Programming Ruby book is awesome but really old. It documents ruby 1.6. I think this is a neat feature of ruby that should be part of the shipped documentation. – Chris Knadler Aug 12 '13 at 19:07
  • I agree with you completely (which is why I accepted your answer :-)) and was only suggesting that you might want to include Programming Ruby book in the "other mentions" section of your answer, particularly since it documents it more effectively than The Ruby Programming Language (imho). – Peter Alfvin Aug 12 '13 at 19:15
  • please trigger me also a notification whenever that will be updated.. please.. :) – Arup Rakshit Aug 12 '13 at 19:16
  • @PeterAlfvin got it! ^_^ I misunderstood. My bad. – Chris Knadler Aug 12 '13 at 21:28
  • Thanks, Chris! I was curious about whether/how percent strings fit in. Do you happen to know if the asymmetric behavior shown is intentional or unavoidable? – Peter Alfvin Aug 18 '13 at 23:25
  • Hey Peter. The problem is the `%` format specifier for strings. If the last string is a percent string, it is ambiguous if it is actually a percent string, or if it's a format specifier. http://ruby-doc.org/core-2.0/String.html#method-i-25 – Chris Knadler Aug 19 '13 at 01:07
  • I'm sure this wasn't intentional. I wouldn't really want percent strings to ever fail contextually. I think it's just a side affect of the language using the `%` for strings in several different ways contextually. – Chris Knadler Aug 19 '13 at 01:09
3

There is a reference in The Ruby Programming Language, page 47.

It looks like it is deliberately in the parser, for situations where you want to split up string literals in code, but don't want to pay the price of concatenating them (and creating 3 or more strings). Long strings with no line breaks, and without requiring line-length busting code, are a good example

text = "This is a long example message without line breaks. " \
    "If it were not for this handy syntax, " \
    "I would need to concatenate many strings, " \
    "or find some other work-around"
Neil Slater
  • 26,512
  • 6
  • 76
  • 94
3

In addition to the pickaxe reference, there are some unit tests:

# compile time string concatenation
assert_equal("abcd", "ab" "cd")
assert_equal("22aacd44", "#{22}aa" "cd#{44}")
assert_equal("22aacd445566", "#{22}aa" "cd#{44}" "55" "#{66}")
Stefan
  • 109,145
  • 14
  • 143
  • 218
0

If you want o break a long single-quoted String-literal across multiple lines without embedding new line in it.

Simply break it into multiple adjacent string literals, the ruby interpreter will concatenate them during the parsing process.

str = "hello" "all"

puts str #=> helloall

remember though, that you must escape the newlines between the literals so that ruby does not interpret the new line as a statement terminator.

str = "hello" \
      " all" \
      " how are you."

puts str #=> hello all how are you
Nic
  • 6,211
  • 10
  • 46
  • 69
Deep Gupta
  • 19
  • 4