Skip to content

Instantly share code, notes, and snippets.

View chumakoff's full-sized avatar

Anton Chumakov chumakoff

View GitHub Profile
@kbaribeau
kbaribeau / gist:1103102
Created July 24, 2011 21:06
Ruby single quote escaping madness
Ok, so I'm trying to replace a ' character with \' in a ruby string. I have working code, but it was
*way* too painful getting there for my taste.
Take a look:
ree-1.8.7-2010.02 :001 > single_quote = "\'"
=> "'"
ree-1.8.7-2010.02 :003 > single_quote.gsub(/'/, "\\\'")
=> ""
ree-1.8.7-2010.02 :004 > single_quote.gsub(/'/) {|c| "\\'"}