Skip to content

Instantly share code, notes, and snippets.

@laser
Created January 17, 2014 16:16
Show Gist options
  • Save laser/8476181 to your computer and use it in GitHub Desktop.
Save laser/8476181 to your computer and use it in GitHub Desktop.
Hash from array of name/value pairs with default value w/no mutation.
a = [["a", 1], ["b", 2]]
h = a.inject Hash.new(9999) do |hash, pair|
hash.merge Hash[[pair]]
end
h["a"] # 1
h["foo"] # 9999
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment