Backslash in string returns two backslash - ruby

I entered this access token value
864876322670016\u00257C4e1d481ecad9eb45b9386745.1-1026038548\u00257CshuA8v7lgo7-hRr2AjbUBd3shek
on a form but it was returned with double backslash like this
864876322670016\\u00257C4e1d481ecad9eb45b9386745.1-1026038548\\u00257CshuA8v7lgo7-hRr2AjbUBd3shek
I'm passing this value to Facebook GraphAPI and this returns an error.
How can I get return replace the double backslash with a single one? or is there a way for the double backslash to not appear?

Are you sure it's actually returned with double backslashes? Internally strings with backslashes will look like they have double backslashes because Ruby is escaping them:
> a = 'aaa\bbb\ccc'
=> "aaa\\bbb\\ccc" # Looks like doubles
> a
=> "aaa\\bbb\\ccc"
> a.inspect
=> "\"aaa\\\\bbb\\\\ccc\"" # Looks even worse
> puts a
aaa\bbb\ccc # ...but it isn't
But if they are double backslashes you can do something like this:
> puts aa
aaa\\bbb\\ccc # String with double backslash
> aa.gsub!("\\\\", "\\")
> puts aa
aaa\bbb\ccc

It's just the way it's being displayed, in escaped form. Your error is likely elsewhere.
This is a common misinterpretation of the output, and a little confusing when you first see it, as Casper has pointed out.
From this question/answer, where the person's issue was essentially the same:
Dang it. I forgot that when the result is displayed in double quotes it shows it escaped.
There's also a short discussion of this perceived issue in this blog post.

Related

Ruby - How do I escape both a back to back backslash and a single

I have the following string, \'e4, which I can't seem to get printed to my IRB console.
My attempts:
"\'e4" gives me back "'e4"
"\\'e4" gives me back "\\'e4"
Any ideas?
Double-quoted strings will always show up with double backslashes in irb due to the way they're escaped.
If you want to see what it actually is:
puts "\\'e4"
# => \'e4
Your first attempt is read as "literal quote e 4" and the second is "literal backslash quote e 4". The second one is correct regardless of how it's displayed.

weird behaviour with single quote escape in irb

puts 'I am 6\'2" tall'
the output is I am 6'2" tall
But in irb
if i types this string 'I am 6\'2" tall' without puts
i got like "I am 6'2\" tall"
Note the position of escape character in the output
Why the position of escape character got changed?
You got different types of quote characters escaped within different types of quotes.
Within single quotes, single quotes need to be escaped to be distinguished from the end of the string, but double quotes need not.
Within double quotes, single quotes need not be escaped, but double quotes need to be.
Because in IRB 'I am 6\'2" tall'.inspect is happening, which is same as puts 'I am 6\'2" tall'

Ruby string with quotes for shell command args?

Hi I need to create string like this:
drawtext="fontfile=/Users/stpn/Documents/Video_Experiments/fonts/Trebuchet_MS.ttf:text='content':fontsize=100:fontcolor=red:y=h/2"
I want to do something like
str = Q%[drawtext="fontfile=/Users/stpn/Documents/Video_Experiments/fonts/Trebuchet_MS.ttf:text='content':fontsize=100:fontcolor=red:y=h/2"]
I am getting this:
=> "drawtext=\"fontfile=/Users/stpn/Documents/Video_Experiments/fonts/Trebuchet_MS.ttf:text='content':fontsize=100:fontcolor=red:y=h/2\""
The escape characters after equals sign in drawtext=" is what I want to get rid of.. How to achieve that?
The string is to be used in a command line args.
Like many languages, Ruby needs a way of delimiting a quoted quote, and the enclosing quotes.
What you're seeing is the escape character which is a way of saying literal quote instead of syntactic quote:
foo = 'test="test"'
# => "test=\"test\""
The escape character is only there because double-quotes are used by default when inspecting a string. It's stored internally as a single character, of course. You may also see these in other circumstances such as a CR+LF delimited file line:
"example_line\r\n"
The \r and \n here correspond with carriage-return and line-feed characters. There's several of these characters defined in ANSI C that have carried over into many languages including Ruby and JavaScript.
When you output a string those escape characters are not displayed:
puts foo
test="test"

Watir magic escape sequence?

I am currently using Watir with Firefox and it seems that when I try to set a field with the following text:
##$QWER7890uiop
The command I am using is the following:
text_field(:name, "password").value=("!##$QWER7890uiop)
I've also tried this:
text_field(:name, "password").set "!##$QWER7890uiop)
Only the first 2 characters get entered. Is there something I can do to by pass this feature?
You need to escape the string using single quotes '.
text_field(:name, "password").value='"!##$QWER7890uiop'
Many characters are substituted inside double quotes.
Escape sequences like \n, \t, \s, etc are replaced by their equivalent character(s). See here for full list.
#{} where anything the braces is interpreted as a ruby expression.
#$something where $something is interpreted as a ruby global variable. That's the problem with your quote above, beside not being terminated.
%s is interpreted as an ERB template expression (it is interpolated).
For instance:
puts "%s hours later" % 'Five'
results in
"Five hours later".

in Ruby, trying to convert those weird quotes into "regular" quotes

I am trying to parse a text file that has the weird quotes like
“ and ” into "normal quotes like "
I tried this:
text.gsub!("“",'"')
text.gsub!("”",'"')
but when it's done, they are still there and show up as
\x93 and \x94
so I tried adding that too with no luck:
text.gsub!('\\x93', '"')
text.gsub!('\\x94', '"')
The problem is, when I try to show those weird quotes on a webpage, it makes that weird diamond with a question mark symbol: �
It seems to work:
text = "“foo”"
=> "\342\200\234foo\342\200\235"
irb(main):002:0> text.gsub!("“",'"')
=> "\"foo\342\200\235"
irb(main):003:0> text.gsub!("”",'"')
=> "\"foo\""
You need to use a hex editor to figure out all the character codes involved.
Re: the second question of why the weird quotes show on a web page as the � symbol:
Your problem is that your web page is not in UTF-8 mode. To get it there, see
http://www.w3.org/International/O-HTTP-charset
If you can't change your web server, add a meta line in the head section of your web pages: http://www.utf-8.com/
Larry
Your first gsubs should work. The reason the second set of gsubs don't work is that you're using single quotes and double backslash. Try the other way around:
text.gsub!("\x93", '"')
text.gsub!("\x94", '"')
You can also do this in one line:
text.gsub!("\x93", '"').gsub!("\x94", '"')
# or
text.gsub!(/(\x93|\x94)/, '"')
Are you sure the encoding of the string is correct?

Resources