Where is the Ruby string matching function officially documented?

I recently realized that if you map a sequence of Ruby string literals (e.g. 'a' "b" 'c' ), this is equivalent to concatenating these string literals. However, I cannot find this language function documented anywhere. I searched for the use of the terms matching and concatenation, but only found a link to it in several StackOverflow answers. Can someone point me the final link?

+12
ruby
Aug 12 '13 at 18:12
source share
4 answers

UPDATE

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

Changes will be propagated to RubyDoc the next time you create documentation.

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, it's nowhere in the official ruby ​​documentation, but I think it should be. As noted in the commentary, the logical place for the documents should be: http://www.ruby-doc.org/core-2.0/doc/syntax/literals_rdoc.html#label-Strings

I opened a transfer request to ruby / ruby with the documentation added.

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

The only other mentions that I found on the Internet:

+9
Aug 12 '13 at 18:59 on
source share
— -

There is a link in the Ruby programming language, page 47 .

It looks like this is intentional in the parser, in situations where you want to split string literals in code, but don't want to pay the price to combine them (and create 3 or more lines). Good example

- long lines without line breaks and without the required code for iterating over lines.
 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" 
+3
Aug 12 '13 at 18:33
source share

In addition to the pickax link , there are several 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}") 
+3
Aug 12 '13 at 18:59 on
source share

If you want o to break a long single-cycling string literal on multiple lines without inserting a new line into it.

Just splitting it into several adjacent string literals, the ruby ​​interpreter will concatenate them during the parsing process.

 str = "hello" "all" puts str #=> helloall 

remember that you must avoid newlines between literals so that ruby ​​does not interpret the newline as a terminator for the statement.

 str = "hello" \ " all" \ " how are you." puts str #=> hello all how are you 
0
Jan 08 '15 at 7:00
source share



All Articles