On Jul 21, 2006, at 1:30 PM, ts wrote:

>>>>>> "J" == Jani Soila <janisoila / gmail.com> writes:
>
> J> irb(main):001:0> s = "a'b'"
> J> => "a'b'"
> J> Unexpected result
> J> irb(main):002:0> s.gsub(/'/, "\\'")
> J> => "ab'b"
>
>  "\\'" is the same than $', i.e. MatchData#post_match. For example
>
> irb(main):001:0> s = "a'b'"
> => "a'b'"
> irb(main):002:0> s.match(/'/).post_match
> => "b'"
> irb(main):003:0>
>
>  ruby replace the first  <'> with <b'>
>               the second <'> with <> (there is nothing after the  
> last ')
>
>
> Guy Decoux
>

What he said.

Also if you don't want to drive yourself crazy with fifteen billion  
backslahes:

str.gsub(/'/) { %q{\'} }