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

后端 未结 4 1640
小鲜肉
小鲜肉 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: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
    

提交回复
热议问题