Substituting apostrphe's

I had a wanted to replace a single apostrophe with an
E.g. “don’t” => “don\'t” (where \ is a single backslash in a

My initial attempt gave strange results …

a = “don’t”
a.sub(/‘/, "\’") # => “dontt”

Backslash-apostrophe is a special replacement pattern that
represents the
part of the string following the match, in this case the letter

It took four backslashes to get it to work.

a.sub(/‘/, "\\’") # => “don\'t”

I’m embarassed to say how long it took me to figure that out.

This works for you? I get a different answer… (NOT in irb, mind

c:\tmp>ruby -v
ruby 1.8.0 (2003-08-04) [i386-mswin32]

c:\tmp>cat tmp.rb
a = “don’t”
puts a.gsub(/'/, “\'”)

c:\tmp>ruby tmp.rb



Do you Yahoo!?
New Yahoo! Photos - easier uploading and sharing.