Jordon Bedwell

Challenge #12

This week’s challenge was reported to have been asked in interviews at Amazon

I’ve never been asked this kind of question. I’ve never interviewed at Amazon though. If that’s the kind of question you are going to ask, let me go ahead and give you an answer ahead of time. Discussion, Repl.it (Javascript), Repl.it (Ruby)Yes even as an experienced programmer I still use CodeAcadamy for fun and practice. You should too, just not the paid version.

Shell Escaping in Ruby

Ruby’s shell escaping quality can sometimes be bad, here how to fix it.

module Utils
  def self.escape(str)
    str = str.gsub(/(\\?[^A-Za-z0-9_\-.,:\/@\n])/) do
      if !$1.start_with?("\\")
        "\\#{$1}" else $1
      end
    end

    str.gsub(/\n/, "'\n'")
  end
end
Utils.escape(Utils.escape("hello\\ world"))
# => "hello\\ world"