简体   繁体   中英

Ruby global variables in Strings being evaluated/truncated

I have some credentials that I need read into Ruby, but the problem is that they have #$ repeated several times in them. The behavior of said character sequence is as follows:

> "123#$123"
=> "123"

For some reason, Ruby evaluates the $1 (which is nil because no regex happened before). Is there any way to avoid this from happening? Thanks.

Use single quote instead of double quote:

irb(main):005:0> '123#$123'    
=> "123\#$123"

Or otherwise, escape the $ char:

irb(main):006:0> "123#\$123"
=> "123\#$123"

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