I'm attempting to use String#gsub to add a slash in front of % or ?, which i'll then be using in a LIKE query. I'm getting some odd behaviour which hopefully someone can explain:
irb(main):018:0> "%?".gsub(/([%\?])/, '\1')
=> "%?"
irb(main):019:0> "%?".gsub(/([%\?])/, '\\1')
=> "%?"
irb(main):020:0> "%?".gsub(/([%\?])/, '\\\1')
=> "\\1\\1"
irb(main):021:0> "%?".gsub(/([%\?])/, '\\\\1')
=> "\\1\\1"
I've worked around this for the moment by just doing two separate gsubs using strings, but i'd quite like to know if anyone can explain what's going on!