Substituting apostrphe's

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

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
‘t’.

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
you)

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
dontt

Hrm.

···

Do you Yahoo!?
New Yahoo! Photos - easier uploading and sharing.
http://photos.yahoo.com/