Created
January 10, 2014 17:55
-
-
Save aarti/8359191 to your computer and use it in GitHub Desktop.
Reverse Key Value in Hash
This file contains hidden or 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
| irb(main):001:0> h = { a: 1, b: 2 } | |
| => {:a=>1, :b=>2} | |
| # each_with_object same as reduce or inject, memo is a new hash. | |
| # unpack arguments in block | |
| irb(main):002:0> h.each_with_object({}) {|(k,v),new_hash| new_hash[v] =k } | |
| => {1=>:a, 2=>:b} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment