[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Escaping characters

Jeremy Woertink

11/7/2007 1:00:00 AM

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
--
Posted via http://www.ruby-....

7 Answers

Pradeep Elankumaran

11/7/2007 1:28:00 AM

0

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, 2007, at 8:00 PM, Jeremy Woertink wrote:

> 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
> --
> Posted via http://www.ruby-....
>


yermej

11/7/2007 1:28:00 AM

0

On Nov 6, 7:00 pm, Jeremy Woertink <jeremywoert...@gmail.com> wrote:
> 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
> --
> Posted viahttp://www.ruby-....

If you actually output the string:

> puts '\\'
=> nil

you should get the result you're expecting.

Todd Benson

11/7/2007 1:41:00 AM

0

On 11/6/07, Jeremy Woertink <jeremywoertink@gmail.com> wrote:
> 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

Jeremy Woertink

11/7/2007 1:50:00 AM

0

yermej wrote:
> On Nov 6, 7:00 pm, Jeremy Woertink <jeremywoert...@gmail.com> 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-....
>
> 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
--
Posted via http://www.ruby-....

Todd Benson

11/7/2007 2:25:00 AM

0

On 11/6/07, Jeremy Woertink <jeremywoertink@gmail.com> wrote:
> yermej wrote:
> > On Nov 6, 7:00 pm, Jeremy Woertink <jeremywoert...@gmail.com> 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-....
> >
> > 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

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

Todd

Justin Collins

11/7/2007 2:27:00 AM

0

Jeremy Woertink wrote:
> yermej wrote:
>
>> On Nov 6, 7:00 pm, Jeremy Woertink <jeremywoert...@gmail.com> 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-....
>>>
>> 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
>
Decent explanation here:

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

-Justin

Phrogz

11/7/2007 4:55:00 AM

0

On Nov 6, 6:28 pm, Pradeep Elankumaran <skyfallsin...@gmail.com>
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.