PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Escaping characters
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Escaping characters

Réponse
 
LinkBack Outils de la discussion
Vieux 07/11/2007, 01h00   #1
Jeremy Woertink
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 07/11/2007, 01h28   #2
Pradeep Elankumaran
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Escaping characters

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-forum.com/.
>



  Réponse avec citation
Vieux 07/11/2007, 01h28   #3
yermej
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Escaping characters

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-forum.com/.


If you actually output the string:

> puts '\\'

\
=> nil

you should get the result you're expecting.

  Réponse avec citation
Vieux 07/11/2007, 01h41   #4
Todd Benson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Escaping characters

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

  Réponse avec citation
Vieux 07/11/2007, 01h49   #5
Jeremy Woertink
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Escaping characters

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

  Réponse avec citation
Vieux 07/11/2007, 02h25   #6
Todd Benson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Escaping characters

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-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


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

Todd

  Réponse avec citation
Vieux 07/11/2007, 02h26   #7
Justin Collins
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Escaping characters

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-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
>

Decent explanation here:

http://blade.nagaokaut.ac.jp/cgi-bin...by-talk/240303

-Justin

  Réponse avec citation
Vieux 07/11/2007, 04h54   #8
Phrogz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Escaping characters

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.

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h55.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,15238 seconds with 16 queries