开发者

Ruby strange quoting

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开发者_StackOverflow中文版(main):067:0> "  \"  " =~ Regexp.new(eval("\"  \\\"  \""))   
=> 0

irb(main):068:0> "  +  " =~ Regexp.new(eval("\"  \\\\+  \""))   
=> 0


When you write \" \+ \\+ \\\+ \\\\+ \" you get " + \+ \+ \\+ ". After, you use eval 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 escaping is perfectly good,

eval = evaluate/execute the string

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜