Created
September 10, 2013 23:23
-
-
Save acook/6517146 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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