单引号和%q只转义\(反斜杠)和‘(单引号),其他不转义
双引号和%Q转义所有。
a1 = 'hello \\world'
a2 = 'hello \nworld'
a3 = 'that\'s nice!'
b1 = "hello \\world"
b2 = "hello \nworld"
b3 = "that\'s nice!"
puts a1
puts a2
puts a3
puts b1
puts b2
puts b3=>
hello \world
hello \nworld
that's nice!
hello \world
hello
world
that's nice!
本文介绍了在编程中使用单引号和双引号定义字符串时的转义规则差异。单引号仅转义反斜杠()和单引号(’),而双引号则转义所有字符。通过具体的例子展示了不同转义规则对字符串的影响。
437

被折叠的 条评论
为什么被折叠?



