Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ruby

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?

like image 608
Peter Alfvin Avatar asked Aug 12 '13 18:08

Peter Alfvin


People also ask

What is a string literal in Ruby?

String literals are simply strings that are created using Ruby's built-in string syntax. This includes those created using double quotes (“), single quotes ('), or the special forms %Q and %q. Traditionally, these forms have all created strings that can be modified.

How do you recognize a string literal?

A "string literal" is a sequence of characters from the source character set enclosed in double quotation marks (" "). String literals are used to represent a sequence of characters which, taken together, form a null-terminated string. You must always prefix wide-string literals with the letter L.

What is an example of a string literal?

A string literal is a sequence of zero or more characters enclosed within single quotation marks. The following are examples of string literals: 'Hello, world!' 'He said, "Take it or leave it."'

Why is it called a string literal?

1. Alternatively referred to as literal(s), a literal string is several characters enclosed in double or single quotes, depending on the programming language or command line. A program does not interpret characters in a literal string until it encounters the next double or single quote.


1 Answers

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:

  • The Ruby Programming Language, page 47 (mentioned in another answer)
  • Ruby Forum Post circa 2008
  • Programming Ruby
like image 56
Chris Knadler Avatar answered Oct 04 '22 09:10

Chris Knadler