Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_extensions.rb

Overview

:nodoc:

Direct Known Subclasses

Automaton::Graph

Instance Method Summary collapse

Instance Method Details

#value_mapObject

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