I have this code:
"1'2".gsub("'","\\'")
Instead of "1\'2", I get: "122". Why?
"1\'2"
"122"
You need to use this:
puts "1'2".gsub("'","\\\\'")
It is because "\\'" means the context following the match, which is "2".
"\\'"
"2"