Method: Hash#traverse

Defined in:
lib/nano/hash/traverse.rb

#traverse(&b) ⇒ Object

Returns a new hash created by traversing the hash and its subhashes, executing the given block on the key and value. The block should return a 2-element array of the form [key, value].

h = { "A"=>"A", "B"=>"B" }
h.traverse { |k,v| [k.downcase, v] }
h  #=> { "a"=>"A", "b"=>"B" }


13
14
15
16
17
18
19
# File 'lib/nano/hash/traverse.rb', line 13

def traverse(&b)
  inject({}) do |h,(k,v)|
    nk, nv = b[k,v]
    h[nk] = ( Hash === v ? v.traverse(&b) : nv )
    h
  end
end