Escaping characters

I don’t understand this.

irb(main):002:0> ‘’’
=> “’”
irb(main):003:0> ‘\’
=> “\”
irb(main):004:0>

I know the backslash escapes a character, so in the first line, I escape
the quote so it will return a string that is a single quote, but in the
second one I would expect it to return “”, instead it returns “\” both
backslashes, and I only want one of them. My actual problem looks like
this:

irb(main):004:0> “(G\D01=Name~\D02=1234~\”
=> “(G\D01=Name~D02=1234~\”

The string that is returned is wrong,but if I do
irb(main):005:0> “(G\D01=Name~\D02=1234~\”
=> “(G\D01=Name~\D02=1234~\”

the string that is returned is still wrong.

~Jeremy

In Ruby, there is a distinction between strings that are between
double quotes and strings in single quotes.
“\” escapes the necessary characters, and also allows substitution.
ex: “#{name}” => “Pradeep”
‘\’ does not do any of these. ex: ‘#{name}’ => ‘#{name}’

Single-quoted strings are faster than double-quoted strings.

  • Pradeep

On Nov 6, 7:00 pm, Jeremy W. [email protected] wrote:

second one I would expect it to return "", instead it returns “\” both
the string that is returned is still wrong.

~Jeremy

Posted viahttp://www.ruby-forum.com/.

If you actually output the string:

puts ‘\’

=> nil

you should get the result you’re expecting.

On 11/6/07, Jeremy W. [email protected] wrote:

I know the backslash escapes a character, so in the first line, I escape
=> “(G\D01=Name~\D02=1234~\”

the string that is returned is still wrong.

s = ‘\’
puts ‘\’ #returns
s.length #returns 1

It’s one byte of value 134 in base 10. What you are seeing is the
representation of it in irb. Like try…

s = "hello
"

Note the return character before the second end quote.

Todd

On 11/6/07, Jeremy W. [email protected] wrote:

puts ‘\’

=> nil

you should get the result you’re expecting.
Rock on.

So basically I had to do \\ just to get \ and \ just to get .
Crazy, but it works so I’m happy.
Thanks

Right. When irb shows you “\”, what it’s showing you is the
double-quoted correct representation of a single backslash.

Todd

yermej wrote:

On Nov 6, 7:00 pm, Jeremy W. [email protected] wrote:

second one I would expect it to return "", instead it returns “\” both
the string that is returned is still wrong.

~Jeremy

Posted viahttp://www.ruby-forum.com/.

If you actually output the string:

puts ‘\’

=> nil

you should get the result you’re expecting.
Rock on.

So basically I had to do \\ just to get \ and \ just to get .
Crazy, but it works so I’m happy.
Thanks

~Jeremy

Jeremy W. wrote:

Rock on.

So basically I had to do \\ just to get \ and \ just to get .
Crazy, but it works so I’m happy.
Thanks

~Jeremy

Decent explanation here:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/240303

-Justin

On Nov 6, 6:28 pm, Pradeep E. [email protected]
wrote:

In Ruby, there is a distinction between strings that are between
double quotes and strings in single quotes.
“\” escapes the necessary characters, and also allows substitution.
ex: “#{name}” => “Pradeep”
‘\’ does not do any of these. ex: ‘#{name}’ => ‘#{name}’

Single-quoted strings are faster than double-quoted strings.

I know it seems like that should be the case, but can you provide any
proof that it is? In all the tests I’ve done, I’ve never found single-
quoted strings to be any bit measurably faster than double-quoted.

Pradeep E. wrote in post #582987:

In Ruby, there is a distinction between strings that are between
double quotes and strings in single quotes.
“\” escapes the necessary characters, and also allows substitution.
ex: “#{name}” => “Pradeep”
‘\’ does not do any of these. ex: ‘#{name}’ => ‘#{name}’

Single-quoted strings are faster than double-quoted strings.

  • Pradeep

Thanks for the explanation!! now i get it!!!