-2

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?

sawa
  • 165,429
  • 45
  • 277
  • 381
ggoha
  • 1,996
  • 3
  • 23
  • 31
  • 1
    It is unclear what your question is. Can you show a piece of code that you are trying to use and is failing? – kabanus Dec 10 '18 at 13:53
  • I read the password from the secrets and it did not work. I try simple string and have behavior like in question – ggoha Dec 10 '18 at 13:57
  • for example ruby -e 'puts "a\\yb"' => "ayb" – ggoha Dec 10 '18 at 13:58
  • 1
    I have your version of ruby and get `a\yb` as output on the terminal. Please post exactly what you are running and is not working so we can better understand. – kabanus Dec 10 '18 at 14:02
  • 1
    After your edits, `p` is the problem. Use `puts` instead. – Conner Dec 10 '18 at 21:29

3 Answers3

4

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.

tadman
  • 208,517
  • 23
  • 234
  • 262
1

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.

Conner
  • 30,144
  • 8
  • 52
  • 73
0

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
Ivan Gurzhiy
  • 229
  • 2
  • 6