Skip to content

Instantly share code, notes, and snippets.

@zetter
Created February 5, 2010 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zetter/295978 to your computer and use it in GitHub Desktop.
Save zetter/295978 to your computer and use it in GitHub Desktop.
class Hash
def hash_map()
result = self.inject({}) do |hash, (k, v)|
hash.merge(yield(k, v))
end
end
end
{:a => 1, :b => 2}.hash_map {|k,v| {k.to_s*2, v*2}} # => {"aa"=>2, "bb"=>4}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment