Created
February 10, 2010 00:27
-
-
Save dlundquist/299860 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
# Returns a new hash applying the supplied transform to all values | |
class Hash | |
def copy_and_transform(&transform) | |
rv = self.class.new | |
self.each do |k,v| | |
case transform.arity | |
when 1 | |
rv[k] = yield(v) | |
when 2 | |
rv[k] = yield(k,v) | |
else | |
raise ArgumentError.new("wrong number of arguments, #{transform.arity} for 1..2") | |
end | |
end | |
return rv | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment