Skip to content

Instantly share code, notes, and snippets.

@dlundquist
Created February 10, 2010 00:27
Show Gist options
  • Save dlundquist/299860 to your computer and use it in GitHub Desktop.
Save dlundquist/299860 to your computer and use it in GitHub Desktop.
# 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