The escape mechanism for handling filenames with single quotes in them is repeating text, not escaping at all. <pre>s = "it's" # Original escaped = s.to_s.gsub('\'', '\'\\\'\'') # => "it's's" %Q{"#{escaped}"} #=> "\"it's's\"" # Patched %Q{"#{s.to_s.gsub("'") { "\\'" }}"} # => "\"it\\'s\"" </pre> Looks like gsub placeholders are kicking in accidentally.