Last active
August 29, 2015 14:03
-
-
Save cheeyeo/8a5ff0354ab021c077fd to your computer and use it in GitHub Desktop.
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 | |
# Returns a new hash with the results of running +block+ once for every value. | |
# The keys are unchanged. | |
# | |
# { a: 1, b: 2, c: 3 }.transform_values { |x| x * 2 } | |
# # => { a: 2, b: 4, c: 6 } | |
def transform_values | |
result = self.class.new | |
each do |key, value| | |
result[key] = yield(value) | |
end | |
result | |
end | |
# Destructive +transform_values+ | |
def transform_values! | |
each do |key, value| | |
self[key] = yield(value) | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment