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 > gsub("\\", "\\\\") seems unintuitive
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
gsub("\\", "\\\\") seems unintuitive

Réponse
 
LinkBack Outils de la discussion
Vieux 22/02/2008, 17h27   #1
John Woods
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut gsub("\\", "\\\\") seems unintuitive

The following confusing behavior is noted in the pickaxe book (2nd ed)
on page 75:

# I would expect two backslashes in the result
irb> puts "\\".gsub("\\","\\\\")
\

# I would expect four backslashes in the result
irb> puts "\\".gsub("\\","\\\\\\\\")
\\

I can certainly work around it, but it seems unintuitive. Is there a
reason why gsub behaves this way? Just curious...

  Réponse avec citation
Vieux 22/02/2008, 17h36   #2
William James
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: gsub("\\", "\\\\") seems unintuitive

On Feb 22, 11:27 am, John Woods <jqwo...@gmail.com> wrote:
> The following confusing behavior is noted in the pickaxe book (2nd ed)
> on page 75:
>
> # I would expect two backslashes in the result
> irb> puts "\\".gsub("\\","\\\\")
> \
>
> # I would expect four backslashes in the result
> irb> puts "\\".gsub("\\","\\\\\\\\")
> \\
>
> I can certainly work around it, but it seems unintuitive. Is there a
> reason why gsub behaves this way? Just curious...


puts "\\".gsub("\\"){"\\\\"}
  Réponse avec citation
Vieux 22/02/2008, 17h37   #3
J. Cooper
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: gsub("\\", "\\\\") seems unintuitive

John Woods wrote:
> The following confusing behavior is noted in the pickaxe book (2nd ed)
> on page 75:
>
> # I would expect two backslashes in the result
> irb> puts "\\".gsub("\\","\\\\")
> \
>
> # I would expect four backslashes in the result
> irb> puts "\\".gsub("\\","\\\\\\\\")
> \\
>
> I can certainly work around it, but it seems unintuitive. Is there a
> reason why gsub behaves this way? Just curious...


It's not a gsub thing, per se--it's a string thing. Backslashes are used
in strings to escape special characters. One such character is a " mark.
If you want to write a " mark in the middle of a string, you have to
escape it with a backslash:
"They call me \"Mellow Yellow\" etc."

If you didn't, then the " would signify the end of the string!
Similarly, in the example you listed, if you just did:
"\"
then you end up with a string that ISN'T ended! Because you escaped the
next ". So, if you want a literal backslash, you have to escape the
backslash too: "\\"

It just looks confusing because you are escaping the escape character


--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 22/02/2008, 17h48   #4
Rob Biedenharn
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: gsub("\\", "\\\\") seems unintuitive

On Feb 22, 2008, at 12:27 PM, John Woods wrote:
> The following confusing behavior is noted in the pickaxe book (2nd
> ed) on page 75:
>
> # I would expect two backslashes in the result
> irb> puts "\\".gsub("\\","\\\\")
> \
>
> # I would expect four backslashes in the result
> irb> puts "\\".gsub("\\","\\\\\\\\")
> \\
>
> I can certainly work around it, but it seems unintuitive. Is there a
> reason why gsub behaves this way? Just curious...



Notwithstanding the earlier responses...

Since the replacement string is evaluated 'twice', once as a ruby
string literal and then again by gsub to look for group refrences like
'\1', you need to provide two levels of escaping for a backslash.

\ is "\\"
so two of them is "\\\\"
and you want gsub to see that so it need to have them escaped: "\\\\\\
\\"

Whew! Yeah, it's unfortunate, but backslash is doing double-duty
here: introducing a group reference to the regular expression and
escaping characters in a string literal (just like "\n", but also
itself).

-Rob

Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com


  Réponse avec citation
Vieux 22/02/2008, 17h53   #5
Kyle Schmitt
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: gsub("\\", "\\\\") seems unintuitive

Just as a side note, this is typical in all real programming languages.
C, C++, Java, Perl, sh, etc.
I _believe_ it's also true in python, lithp/scheme, & (o)caml, but for
those I've either not used them, or not used them in so long I'm
unsure.

Some languages, like vb{6|script|.net}, use a doubled quote, but those
aren't really proper programing languages

--Kyle

  Réponse avec citation
Vieux 22/02/2008, 18h01   #6
John Woods
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: gsub("\\", "\\\\") seems unintuitive

Thanks Rob, that's exactly what I was missing -- the second round of
escaping is necessary to make escaped references to regex groups work.


-----Original Message-----
From: Rob Biedenharn
Sent: 02/22/2008 09:48 AM
> On Feb 22, 2008, at 12:27 PM, John Woods wrote:
>> The following confusing behavior is noted in the pickaxe book (2nd ed)
>> on page 75:
>>
>> # I would expect two backslashes in the result
>> irb> puts "\\".gsub("\\","\\\\")
>> \
>>
>> # I would expect four backslashes in the result
>> irb> puts "\\".gsub("\\","\\\\\\\\")
>> \\
>>
>> I can certainly work around it, but it seems unintuitive. Is there a
>> reason why gsub behaves this way? Just curious...

>
>
> Notwithstanding the earlier responses...
>
> Since the replacement string is evaluated 'twice', once as a ruby string
> literal and then again by gsub to look for group refrences like '\1',
> you need to provide two levels of escaping for a backslash.
>
> \ is "\\"
> so two of them is "\\\\"
> and you want gsub to see that so it need to have them escaped: "\\\\\\\\"
>
> Whew! Yeah, it's unfortunate, but backslash is doing double-duty here:
> introducing a group reference to the regular expression and escaping
> characters in a string literal (just like "\n", but also itself).
>
> -Rob
>
> Rob Biedenharn http://agileconsultingllc.com
> Rob@AgileConsultingLLC.com
>
>
>



  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 19h54.


É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,12678 seconds with 14 queries