"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{\\\\'})
> \'ello
> 
> Note:
> 
> >> 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.

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?
 
-- 
Simon Strandgaard