简体   繁体   English

Ruby字符串以'\\'字符开头

[英]Ruby string prepend '\' character

why ruby is prepends '\\' character while I am trying to run below code. 为什么在我尝试在代码下运行时,ruby前面加了“ \\”字符。 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. 由于#$foo可以解释为全局变量$foo插值,因此必须对#字符进行转义。 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. 更精确地说,不可能使用字符串"#$"$是无效的全局变量)或"#$123"$123是无效的全局变量)进行插值,但是它使检查算法或插值算法成为可能。检查#$之后的序列很复杂,所以我想这就是为什么即使在这种情况下也转义了#原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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