这六个 Ruby 字符串语法之间有什么区别,它们是连接、插值还是其他

What's the difference between these six Ruby string syntaxes, are they concatenation, interpolation or maybe something else

我试图跨多行写一个字符串,因为它太长了,我到达了 this solution that I think looks best but I couldn't find anything about it in the Ruby documentation

    my_original = 'what I originally had ' +
                  'across multiple lines'       
    # executes to:  "what I originally had across multiple lines"

    new_style = 'new format to '\
                'span multiple lines'
    # executes to: "new format to span multiple lines\n"

但是我看到有很多方法可以做到这一点,我想知道他们是使用连接还是插值,all I could find was this.在这种情况下,性能并不是特别重要,但了解会发生什么在幕后不会受伤。所以想知道它们之间的区别。

        my_original = 'what I originally had ' +
                     'across multiple lines' 
        # executes to: "what I originally had across multiple lines"

        s_one = 'I assume this is a more ' 
        s_two = 'verbose version of the '
        s_three = 'first example.'
        my_string = s_one + s_two + s_three       
        # executes to: "I assume this is a more verbose version of the first example."

        my_first_solution = 'that breaks whenever' 
        my_first_solution << 'ruby 3.0 might be released'
        # executes to: 

        style_i_used = 'which can span multiple '\
                       'lines without having '\
                       'extra white space' 
        # executes to: "which can span multiple lines without having extra white space"

        another_string = <<-HEREDOC
when you don't mind
really wonky indentation 
        or having extra spaces.
        HEREDOC
        # executes to:"when you don't mind \nreally wonky indentation \n        or having extra spaces\n        and new lines. \n"
        # "Is this just a one line string with no special characters?# executes to:

        another_string = <<~HEREDOC
        I assume this is the same as 
        the non squiggly version with 
        stripping between lines.
        HEREDOC
        # executes to: "I assume this is the same as \nthe non squiggly version with \nstripping between lines.\n"

       #Edit
       another_one =
       "I did not originally include; however,
       this one also adds a bunch of extra 
       white space and new lines."
       # executes to:"I did not originally include; however,\n       this one also adds a bunch of extra \n       white space and new lines." 
another_one =
"what I originally had
across multiple lines"

这是一个NoMethodError

style_i_used = 'a format to' /
               'span multiple lines'

/ 是字符串没有的方法调用。 \ 另一方面 "escapes" 换行符,因此它被视为一个字符串。

这些不会出错,但是不好:

my_original = 'what I originally had' +
              'across multiple lines' 

s_one = 'I assume this is the same as ' 
s_two = 'the first example with the '
s_three = 'code being more verbose'
my_string = s_one + s_two + s_three       

my_first_solution = 'that breaks whenever' 
my_first_solution << 'ruby 3.0 might be released'

+<< 是方法调用。我认为 Ruby 解析器不够聪明,无法看到这些都可以是编译时字符串,因此这些字符串实际上是在 运行-time[=30= 期间构建的] 与对象分配和一切。那是完全没有必要的。

其他种类不一样usage/results,你用哪个合适。

str = "a"\
      "b"  # result is "ab"

str = "a\
b"         # also "ab"

str = "a
b"         # result is "a\nb"

str = <<END
  '"a'"
  '"b'"
END
# result is "  '\"a'\"\n  '\"b'\""

str = <<~END
  '"a'"
  '"b'"
END
# result is "'\"a'\"\n'\"b'\""