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"

Bash completion in Ruby

Your auto-completion is kind-of complicated. You build it entirely in bash with the assumption that it provides more perf (and it probably does… it probably isn’t to-be honest… based on what I can gander from looking at it.) It’s not maintainable even if it does have more perf. Why not use Ruby to generate the auto complete list for Ruby?