简体   繁体   中英

Ruby string prepend '\' character

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM