Created
January 18, 2012 14:37
-
-
Save visibletrap/1633269 to your computer and use it in GitHub Desktop.
"do Tap"
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
def do_each(hash) | |
result = {} | |
hash.each { |key, value| result[key] = do_something(value) } | |
result | |
end | |
def do_inject(hash) | |
hash.inject({}) { |result, pair| result[pair.first] = do_something(pair.last); result } | |
end | |
def do_tap(hash) | |
{}.tap do |result| | |
hash.each { |key, value| result[key] = do_something(value) } | |
end | |
end | |
def do_map(hash) | |
Hash[hash.map { |key, value| [key, do_something(value)] }] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment