I remember when I switched to Ruby, I had difficulties understanding
the escaping rules in conjunction with single/double quotes.
String#gsub got me really confused.
I think I read somewhere that perl6 uses $ in the regexp replacement string,
in order to avoid our escaping hell. I think this is a good initiative.
So escape can be used freely:
“hello”.gsub!(/h/, %q|'|) #-> 'ello
“hello”.gsub!(/(.)$1/, ‘$&$1’) #-> helllo
You need an escaped backslash to prevent interpretation of it in the
replacement pattern.
I remember when I switched to Ruby, I had difficulties understanding
the escaping rules in conjunction with single/double quotes.
String#gsub got me really confused.
It took some time for me, too.
I think I read somewhere that perl6 uses $ in the regexp replacement
string,
in order to avoid our escaping hell. I think this is a good initiative.
So escape can be used freely:
“hello”.gsub!(/h/, %q|'|) #-> 'ello
“hello”.gsub!(/(.)$1/, ‘$&$1’) #-> helllo
Maybe we should do an RCR… use dollar in regexp?
IMHO that would break too much code.
After all quoting rules are very logical: Step 1: “” and ‘’ quoting rules
apply. Step 2: Then the gsub quoting rules apply to the result of step 1.
IMHO confusion often results from testing in irb which will print an
escaping backslash before each backslash in a string thus irritating people
about the contents of the string printed:
a=%q{\}
=> “\”
puts a
=> nil
Another source of confusion is the lax treatment of backslashes:
a=%q{\1}
=> “\1”
puts a
\1
=> nil
a=%q{\1}
=> “\1”
puts a
\1
=> nil
Normally you would have to use the second form only, that would be more
logical IMHO.
I think I read somewhere that perl6 uses $ in the regexp replacement string,
in order to avoid our escaping hell. I think this is a good initiative.
So escape can be used freely:
“hello”.gsub!(/h/, %q|'|) #-> 'ello
“hello”.gsub!(/(.)$1/, ‘$&$1’) #-> helllo
Maybe we should do an RCR… use dollar in regexp?
Hmmm… I thought Ruby was moving more in the direction of getting rid
of Perl-like global variables But anyway, I think the thing you’re
describing might lead to some awkward things. I think it’s more logical
to treat $1 as just a regular variable for purposes of compiling the
regular expression:
I think I read somewhere that perl6 uses $ in the regexp replacement string,
in order to avoid our escaping hell. I think this is a good initiative.
So escape can be used freely:
“hello”.gsub!(/h/, %q|'|) #-> 'ello
“hello”.gsub!(/(.)$1/, ‘$&$1’) #-> helllo
Maybe we should do an RCR… use dollar in regexp?
Hmmm… I thought Ruby was moving more in the direction of getting rid
of Perl-like global variables But anyway, I think the thing you’re
describing might lead to some awkward things. I think it’s more logical
to treat $1 as just a regular variable for purposes of compiling the
regular expression:
If your change were made, $1 would be nil in the last line because
there are no captures.
And here, you’d get a kind of postponed compilation of re:
/(abc)/.match(“abc”)
re = /(blah)#{$1}def/
Sorry for the dumb examples… but I think the problems would be real.
At first sight I wondered what you are talking about.
I have never done any perl programming in my life.
IIUC perl interpolates $ inside strings… that gave me a clue to
what you are talking about.
Ruby should of cause not interpolate global variables!
hmm? I think the suggestion has nothing to do with the #{…} construct
for interpolation. I think Simon is suggesting allowing “$1$2” as an
alternative to “\1\2”. These are things that would never be
interpolated in current scheme. I think this would make things clearer,
much less confusing. The backslash escaping rules are complicated
enough that I never use them; I only use the block form of sub and
gsub.
note that
/(\d+)$1/.match(str)
would be entirely different from
/(\d+)#{$1}/.match(str)
as the first would match something like this: “12341234” and the second
would match “1276#{$1}”, with whatever $1 is interpolated as.
cheers,
Mark
···
On May 31, 2004, at 5:33 AM, David Alan Black wrote:
I think I read somewhere that perl6 uses $ in the regexp replacement
string,
in order to avoid our escaping hell. I think this is a good
initiative.
So escape can be used freely:
“hello”.gsub!(/h/, %q|'|) #-> 'ello
“hello”.gsub!(/(.)$1/, ‘$&$1’) #-> helllo
Maybe we should do an RCR… use dollar in regexp?
Hmmm… I thought Ruby was moving more in the direction of getting rid
of Perl-like global variables But anyway, I think the thing you’re
describing might lead to some awkward things. I think it’s more
logical
to treat $1 as just a regular variable for purposes of compiling the
regular expression:
At first sight I wondered what you are talking about.
I have never done any perl programming in my life.
IIUC perl interpolates $ inside strings… that gave me a clue to
what you are talking about.
I’m not sure why It was pure Ruby, not related to Perl.
Ruby should of cause not interpolate global variables!
Now I’m confused… Why not? Or do you mean interpolate without
#{…} ?
At first sight I wondered what you are talking about.
I have never done any perl programming in my life.
IIUC perl interpolates $ inside strings… that gave me a clue to
what you are talking about.
I’m not sure why It was pure Ruby, not related to Perl.
Ruby should of cause not interpolate global variables!
Now I’m confused… Why not? Or do you mean interpolate without
#{…} ?
Agree we have lost syncronization here
Mark Hubbart has made an reply where he talk about the $ idea,
that mail describes exactly what I had in mind to say. http://ruby-talk.org/101910