Ruby string prepend '\' character

我怕爱的太早我们不能终老 提交于 2019-12-02 19:44:01

问题


why ruby is prepends '\' character while I am trying to run below code. It is happening with only '#$'

It is happening with all ruby version.

puts '#$'   => '\#$'

or

'#$'  => '\#$'

or

'mypassord#$123'  =>  'mypassord\#$123'

Please share you experience here. Is it a ruby problem or anything?


回答1:


No it is not a ruby problem. It is your problem. Since #$foo can be interpreted as interpolation of the global variable $foo, it is necessary to escape the # character. That is why there is a backslash.

To be more precise, there is no possibility of interpolation with the string "#$" ($ is an invalid global variable) or "#$123" ($123 is an invalid global variable), but it makes the inspection algorithm or the interpolation algorithm complicated to check the sequence after #$, so I guess that is why # is escaped even in such cases.



来源:https://stackoverflow.com/questions/24825136/ruby-string-prepend-character

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!