Ruby中关于单引号和双引号的转义

时间:2021-09-09 06:36:53

单引号和%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!