Skip to content

Instantly share code, notes, and snippets.

@acook
Created September 10, 2013 23:23
Show Gist options
  • Save acook/6517146 to your computer and use it in GitHub Desktop.
Save acook/6517146 to your computer and use it in GitHub Desktop.
module Enumerable
# operates like Enumerable#map, but returns a hash
def hmap
inject Hash.new do |new_hash, (k,v)|
new_k, new_v = yield k, v
new_hash[new_k] = new_v
new_hash
end
end
end
h = {a: 1, b: 2}
h.hmap{|k,v| [v, k]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment