Ruby 奇怪的引用
抱歉英语不好。为什么Ruby 的引用如此奇怪?或者这可能是一个错误?
irb(main):027:0> p eval "\" \+ \\+ \\\+ \\\\+ \\\\\+ \""
生产
=> " + + + \\+ \\+ "
或
irb(main):027:0> puts eval "\" \+ \\+ \\\+ \\\\+ \\\\\+ \""
生产
=> + + + \+ \+
或另一个例子
irb(main):067:0> " \" " =~ Regexp.new(eval("\" \\\" \""))
=> 0
irb(main):068:0> " + " =~ Regexp.new(eval("\" \\\\+ \""))
=> 0
sorry for bad English. Why Ruby quoting so strange? Or may be this is a bug?
irb(main):027:0> p eval "\" \+ \\+ \\\+ \\\\+ \\\\\+ \""
produces
=> " + + + \\+ \\+ "
or
irb(main):027:0> puts eval "\" \+ \\+ \\\+ \\\\+ \\\\\+ \""
produces
=> + + + \+ \+
or another example
irb(main):067:0> " \" " =~ Regexp.new(eval("\" \\\" \""))
=> 0
irb(main):068:0> " + " =~ Regexp.new(eval("\" \\\\+ \""))
=> 0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您编写
\" \+ \\+ \\\+ \\\\+ \"
时,您会得到" + \+ \+ \\+ "
。之后,您使用eval
执行此字符串,其中包含另一个双引号字符串。然后,您得到+ + + \+
。\\
=> <代码>\\x
=>x
(se não 表示 nenhum caso especial,como\n
)When you write
\" \+ \\+ \\\+ \\\\+ \"
you get" + \+ \+ \\+ "
. After, you useeval
to execute this string, that contains another double-quoted string. You get, then,+ + + \+
.\\
=>\
\x
=>x
(se não for nenhum caso especial, como\n
)ruby 转义非常好,
eval = 评估/执行字符串
ruby escaping is perfectly good,
eval = evaluate/execute the string