Mar 28, 2011

Ruby has a weird mechanism of escaping a single quote and a double quote

Just don't try to escape a single quote. It's super complicated.

With a double quote, it is a little bit easier. Here's the cheat sheet:


'test " test'.gsub('"', '\\"')
# we got 'test \\\" test'

'test " test'.gsub('"', '"')
# we got 'test \" test'


Amazing, huh ?