I have a password that contains "\y"
. I get:
"a\yb" # => "ayb"
'a\yb' # => 'a\\yb'
"a\\yb" # => "a\\yb"
'a\\b' # => "a\\yb"
And nothing (like concatenation
or sub
) works.
Is there a way to not change the password?
I have a password that contains "\y"
. I get:
"a\yb" # => "ayb"
'a\yb' # => 'a\\yb'
"a\\yb" # => "a\\yb"
'a\\b' # => "a\\yb"
And nothing (like concatenation
or sub
) works.
Is there a way to not change the password?
When you say:
'a\yb'
Then you get this back:
"a\\yb"
These are identical as far as Ruby is concerned. Inside of double quotes ("
) the backslash has special meaning. A single backslash is used to indicate either control codes like \n
meaning newline, or literal versions of same, like \\
meaning literal backslash.
Try this:
puts "a\\yb"
Then you'll see exactly what you want. The \\
part is just escaping.
When you use p
you're calling:
puts "a\\yb".inspect
This inspect
part puts it back into a double-quoted and escaped string, which is where you're getting confused. Print the string, not the "inspected" version of it.
Works for me. Can you show us some context?
ruby -v
#=> ruby 2.5.3p105 (2018-10-18 revision 65156) [x86_64-linux]
ruby -e 'puts "a\\yb"'
#=> a\yb
After your edits, the problem is using p
instead of puts
. For instance:
ruby -e 'puts "a\\yb"'
#=> a\yb
ruby -e 'p "a\\yb"'
#=> "a\\yb"
See this discussion.
In simple words p
print obj.inspect
to output
Simple way to see this:
irb(main):009:0> string = %q{a\yb}
=> "a\\yb"
irb(main):018:0> puts string.inspect
"a\\yb"
irb(main):019:0> p string
"a\\yb"
irb(main):010:0> puts string
a\yb