Escaping characters

  • Thread starter Jeremy Woertink
  • Start date
J

Jeremy Woertink

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
 
P

Pradeep Elankumaran

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
 
Y

yermej

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

If you actually output the string:
puts '\\'
\
=> nil

you should get the result you're expecting.
 
T

Todd Benson

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.

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
 
J

Jeremy Woertink

yermej said:
If you actually output the string:

\
=> 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
 
T

Todd Benson

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
 
P

Phrogz

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top