Method: Hash#traverse
- Defined in:
- lib/core/facets/hash/traverse.rb
#traverse(&block) ⇒ 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", "C"=>{"X"=>"X"}}
g = h.traverse{ |k,v| [k.downcase, v] }
g #=> {"a"=>"A", "b"=>"B", "c"=>{"x"=>"X"}}
NOTE: Hash#traverse is the same as ‘recursive.graph` and might be deprecated in the future (if it ever works!)
CREDIT: Trans
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/core/facets/hash/traverse.rb', line 18 def traverse(&block) inject({}) do |h,(k,v)| if Hash === v v = v.traverse(&block) elsif v.respond_to?(:to_hash) v = v.to_hash.traverse(&block) end nk, nv = block.call(k,v) h[nk] = nv h end end |