I'm trying to backslash-escape a filename that has backticks in it, to pass to some shell commands. I am seeing some weird behavior when trying to use gsub for this.
Some examples in IRB:
irb(main):001:0> 'weird`things'.gsub('`', '\`')
=> "weirdweirdthings"
irb(main):002:0> 'weird`things'.gsub('`', '\\`')
=> "weirdweirdthings"
irb(main):003:0> 'weird`things'.gsub('`', '\\\`')
=> "weird\\`things" # this is what I want
irb(main):004:0> 'weird`things'.gsub('`', '\\\\`')
=> "weird\\`things"
irb(main):005:0> 'weird`things'.gsub('`', '\\\\\`')
=> "weird\\weirdthings"
What is going on here? Does this have something to do with the $` global variable?