Ruby gsub doesn't escape single-quotes
This will also do it, and it's a bit more readable:
def escape_single_quotes(str) str.gsub(/'/) { |x| "\\#{x}" }end
If you want to escape both a single-quote and a backslash, so that you can embed that string in a double-quoted ruby string, then the following will do that for you:
def escape_single_quotes_and_backslash(str) str.gsub(/\\|'/) { |x| "\\#{x}" }end