"David A. Black" <dblack@wobblini.net> schrieb im Newsbeitrag
news:Pine.LNX.4.61.0504280320190.13336@wobblini...
Hi --
>
> "Nicholas Seckar" <nseckar@gmail.com> schrieb im Newsbeitrag
> news:6bfb9c8a050427193967475977@mail.gmail.com...
>>> (You don't really need the block arg (match)
>>
>> Good point
>>
>>> pious".gsub(/([aeiou])(?=[aeiou])/) { "#{$1}'#{$2}" }
>>
>> We could also do away with the block altogether and use
>> "pious".gsub(/([aeiou])(?=[aeiou])/, '\1\'\2')
>>
>> Although I don't know if one approach is preferred to the other.
>
> Yes, it's preferred because it's faster. Although your variant works,
I
> usually prefer to put the correct number of backslashes in there:
>
> "pious".gsub(/([aeiou])(?=[aeiou])/, '\\1\'\\2')
Why do you consider that more correct?
Although both produce the same:
p '\1'
"\\1"
=> nil
p '\\1'
"\\1"
=> nil
because Ruby is so kind to take "\1" literally (i.e not using the
backslash as escaping char because "\1" is not an escaping sequence). I
prefer to explicitely escape the backslash and put the "1" in there
literally. IMHO it's more fail safe when changes are done (especially
when changing single quotes to double quotes, see below).
Consider also
p "\n"
"\n"
=> nil
p "\\n"
"\\n"
=> nil
p "\1"
"\001"
=> nil
p "\\1"
"\\1"
=> nil
Might be overly cautious but this is the kind of error that bites you and
if you're not lucky it can take quite some time to figure what's going on
here.
Kind regards
robert
···
On Thu, 28 Apr 2005, Robert Klemme wrote:
>> On 4/27/05, David A. Black <dblack@wobblini.net> wrote:
"David A. Black" <dblack@wobblini.net> schrieb im Newsbeitrag
news:Pine.LNX.4.61.0504280320190.13336@wobblini...
Hi --
On Thu, 28 Apr 2005, Robert Klemme wrote:
Yes, it's preferred because it's faster. Although your variant works,
I
usually prefer to put the correct number of backslashes in there:
"pious".gsub(/([aeiou])(?=[aeiou])/, '\\1\'\\2')
Why do you consider that more correct?
Although both produce the same:
p '\1'
"\\1"
=> nil
p '\\1'
"\\1"
=> nil
because Ruby is so kind to take "\1" literally (i.e not using the
backslash as escaping char because "\1" is not an escaping sequence). I
prefer to explicitely escape the backslash and put the "1" in there
literally. IMHO it's more fail safe when changes are done (especially
when changing single quotes to double quotes, see below).
If you're talking about double quotes then clearly it has to be "\\1".
I meant specifically in the case of single quotes. Anyway, it's not a
big deal -- I was just curious.
"David A. Black" <dblack@wobblini.net> schrieb im Newsbeitrag
news:Pine.LNX.4.61.0504280509220.30582@wobblini...
Hi --
>
> "David A. Black" <dblack@wobblini.net> schrieb im Newsbeitrag
> news:Pine.LNX.4.61.0504280320190.13336@wobblini...
>> Hi --
>>
>>> Yes, it's preferred because it's faster. Although your variant
works,
> I
>>> usually prefer to put the correct number of backslashes in there:
>>>
>>> "pious".gsub(/([aeiou])(?=[aeiou])/, '\\1\'\\2')
>>
>> Why do you consider that more correct?
>
> Although both produce the same:
>
>>> p '\1'
> "\\1"
> => nil
>>> p '\\1'
> "\\1"
> => nil
>
> because Ruby is so kind to take "\1" literally (i.e not using the
> backslash as escaping char because "\1" is not an escaping sequence).
I
> prefer to explicitely escape the backslash and put the "1" in there
> literally. IMHO it's more fail safe when changes are done (especially
> when changing single quotes to double quotes, see below).
If you're talking about double quotes then clearly it has to be "\\1".
I meant specifically in the case of single quotes.
Well, yes. But quotes don't necessarily stay single through the course of
a software's lifecycle - with all this dating and matching around...
Sorry, drifting off-topic.
Anyway, it's not a
big deal -- I was just curious.
Thought so. I hope I could satisfy your curiosity.
Kind regards
robert
···
On Thu, 28 Apr 2005, Robert Klemme wrote:
>> On Thu, 28 Apr 2005, Robert Klemme wrote: