Skip to content

Instantly share code, notes, and snippets.

@aarti
Created January 10, 2014 17:55
Show Gist options
  • Select an option

  • Save aarti/8359191 to your computer and use it in GitHub Desktop.

Select an option

Save aarti/8359191 to your computer and use it in GitHub Desktop.
Reverse Key Value in Hash
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