Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/ruby_extensions.rb
Overview
:nodoc:
Direct Known Subclasses
Instance Method Summary collapse
-
#value_map ⇒ Object
Invokes block once for each pair of self, each time yielding a new key, value pair.
Instance Method Details
#value_map ⇒ Object
Invokes block once for each pair of self, each time yielding a new key, value pair. Returns a new hash with the values of the original hash replaced by those returned from the block.
{:a => 1, :b => 2}.key_value_map{|key, value| value * 2}
#=> {:a => 2, :a => 4}
29 30 31 32 33 34 |
# File 'lib/ruby_extensions.rb', line 29 def value_map self.inject(self.class.new) do |hash, (key,value)| hash[key] = yield(key, value) hash end end |