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

后端 未结 4 1613
小鲜肉
小鲜肉 2020-11-28 14:52

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 lit

相关标签:
4条回答
  • 2020-11-28 15:05

    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
    0 讨论(0)
  • 2020-11-28 15:09

    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}")
    
    0 讨论(0)
  • 2020-11-28 15:15

    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
    
    0 讨论(0)
  • 2020-11-28 15:23

    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"
    
    0 讨论(0)
提交回复
热议问题