Dear Ruby-hackers,
simple problem: replace a certain letter with ’ (backslash apostrophe).
···
#!/usr/bin/ruby -w
puts “hello”.gsub!(/h/,"\’")
result: elloello
I would like to get:
'ello
What should I try?
Patrick
Dear Ruby-hackers,
simple problem: replace a certain letter with ’ (backslash apostrophe).
result: elloello
I would like to get:
'ello
What should I try?
Patrick
Patrick Gundlach wrote:
Dear Ruby-hackers,
simple problem: replace a certain letter with ' (backslash apostrophe).
#!/usr/bin/ruby -w
puts “hello”.gsub!(/h/,“\'”)result: elloello
I would like to get:
'ello
What should I try?
puts “hello”.gsub!(/h/,“\\'”)
“\” is reserved for backreferences.
–
http://www.mikrocontroller.net - Das Mikrocontroller-Forum
Andreas Schwarz wrote:
Patrick Gundlach wrote:
Dear Ruby-hackers,
simple problem: replace a certain letter with ' (backslash apostrophe).
#!/usr/bin/ruby -w
puts “hello”.gsub!(/h/,“\'”)result: elloello
I would like to get:
'ello
What should I try?
puts “hello”.gsub!(/h/,“\\'”)
“\” is reserved for backreferences.
In the above case ' (slash quote) are interpreted as post-match.
Other useful things in replacement string:
` pre-match
+ last-capture
& match
–
Simon Strandgaard
Andreas Schwarz usenet@andreas-s.net writes:
puts “hello”.gsub!(/h/,“\\'”)
“\” is reserved for backreferences.
Hello Andreas,
Oh, yes. This works fine. Thank you (and of course Simon).
Patrick
or
puts “hello”.gsub!(/h/) { %q(') }
Kristof
On Sun, 30 May 2004 21:11:18 +0200, Andreas Schwarz wrote:
Patrick Gundlach wrote:
Dear Ruby-hackers,
simple problem: replace a certain letter with ' (backslash apostrophe).
#!/usr/bin/ruby -w
puts “hello”.gsub!(/h/,“\'”)result: elloello
I would like to get:
'ello
What should I try?
puts “hello”.gsub!(/h/,“\\'”)
“\” is reserved for backreferences.
Another way to obtain same result… without extra escaping.
puts “hello”.gsub!(/h/) {|m| “\'”}
Patrick Gundlach clr1.10.randomuser@spamgourmet.com wrote:
Andreas Schwarz usenet@andreas-s.net writes:
puts “hello”.gsub!(/h/,“\\'”)
“\” is reserved for backreferences.
Hello Andreas,
Oh, yes. This works fine. Thank you (and of course Simon).
–
Simon Strandgaard
“Simon Strandgaard” neoneye@adslhome.dk schrieb im Newsbeitrag
news:20040530221337.39b1ecfa.neoneye@adslhome.dk…
Andreas Schwarz usenet@andreas-s.net writes:
puts “hello”.gsub!(/h/,“\\'”)
“\” is reserved for backreferences.
Hello Andreas,
Oh, yes. This works fine. Thank you (and of course Simon).
Another way to obtain same result… without extra escaping.
puts “hello”.gsub!(/h/) {|m| “\'”}
Or
puts “hello”.gsub!(/h/, %q{\\'})
'ello
Note:
puts %q{\\‘}
\’
You need an escaped backslash to prevent interpretation of it in the
replacement pattern.
robert
Patrick Gundlach clr1.10.randomuser@spamgourmet.com wrote:
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
Maybe we should do an RCR… use dollar in regexp?
“Robert Klemme” bob.news@gmx.net wrote:
“Simon Strandgaard” neoneye@adslhome.dk schrieb im Newsbeitrag
puts “hello”.gsub!(/h/) {|m| “\'”}
Or
puts “hello”.gsub!(/h/, %q{\\'})
'elloNote:
puts %q{\\‘}
\’You need an escaped backslash to prevent interpretation of it in the
replacement pattern.
–
Simon Strandgaard
“Simon Strandgaard” neoneye@adslhome.dk schrieb im Newsbeitrag
news:20040531140023.1c0d4fe9.neoneye@adslhome.dk…
“Simon Strandgaard” neoneye@adslhome.dk schrieb im Newsbeitrag
puts “hello”.gsub!(/h/) {|m| “\'”}
Or
puts “hello”.gsub!(/h/, %q{\\'})
'elloNote:
puts %q{\\‘}
\’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’) #-> hellloMaybe 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.
Regards
robert
“Robert Klemme” bob.news@gmx.net wrote:
Hi –
Simon Strandgaard neoneye@adslhome.dk writes:
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’) #-> hellloMaybe 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:
/(def)/.match(“abcdef”)
puts $1 # def
/ABC#{$1.upcase}/.match(“whatever”)
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.
David
–
David A. Black
dblack@wobblini.net
David Alan Black wrote:
Simon Strandgaard neoneye@adslhome.dk writes:
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’) #-> hellloMaybe 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 variablesBut 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:/(def)/.match(“abcdef”)
puts $1 # def
/ABC#{$1.upcase}/.match(“whatever”)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! ![]()
–
Simon Strandgaard
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:
Hi –
Simon Strandgaard neoneye@adslhome.dk writes:
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’) #-> hellloMaybe 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 variablesBut 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:/(def)/.match(“abcdef”)
puts $1 # def
/ABC#{$1.upcase}/.match(“whatever”)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.
Hi –
Simon Strandgaard neoneye@adslhome.dk writes:
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
#{…} ?
David
–
David A. Black
dblack@wobblini.net
David Alan Black wrote:
Simon Strandgaard neoneye@adslhome.dk writes:
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
–
Simon Strandgaard