Created
March 13, 2014 12:13
-
-
Save apeiros/9527275 to your computer and use it in GitHub Desktop.
Implement Hash#map_keys, #map_keys!, #map_values, #map_values!, #map_pairs and #map_pairs!
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
class Hash | |
def map_keys | |
map_pairs { |key, value| [yield(key), value] } | |
end | |
def map_keys! | |
map_pairs! { |key, value| [yield(key), value] } | |
end | |
def map_values | |
map_pairs { |key, value| [key, yield(value)] } | |
end | |
def map_values! | |
map_pairs! { |key, value| [key, yield(value)] } | |
end | |
def map_pairs(&block) | |
Hash[map(&block)] | |
end | |
def map_pairs!(&block) | |
replace(map_pairs(&block)) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment