开发者

Ruby strange quoting

开发者 https://www.devze.com 2023-02-05 03:35 出处:网络
sorry for bad English. Why Ruby quoting so strange? Or may be this is a bug? irb(main):027:0> p eval \"\\\"\\+\\\\+\\\\\\+\\\\\\\\+\\\\\\\\\\+\\\"\"

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

精彩评论

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