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", { "X"=>"X" } }
h.recursive_each{ |k,v| p [k.downcase, v] }
produces
["a", "A"] ["b", "B"] ["x", "X"]
CREDIT: Trans
# File lib/core/facets/hash/recursive.rb, line 59
59: def each(&block)
60: @enum.each do |k,v|
61: if Hash === v
62: v = v.recursive.each(&block)
63: elsif v.respond_to?(:to_hash)
64: v = v.to_hash.recursive.each(&block)
65: end
66: block.call(k,v)
67: end
68: #@enum.each do |k,v|
69: # if Hash === v
70: # v.recursive.each(&block)
71: # else
72: # block.call(k,v)
73: # end
74: #end
75: end
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", {"X"=>"X"}}
g = h.recursive_graph{ |k,v| [k.downcase, v] }
g #=> {"a"=>"A", "b"=>"B", {"x"=>"X"}}
CREDIT: Trans
# File lib/core/facets/hash/recursive.rb, line 120
120: def graph(&block)
121: @enum.inject({}) do |h,(k,v)|
122: if Hash === v
123: v = v.recursive.graph(&block)
124: elsif v.respond_to?(:to_hash)
125: v = v.to_hash.recursive.graph(&block)
126: end
127: nk, nv = block.call(k,v)
128: h[nk] = nv
129: h
130: end
131: end
In place version of traverse, which traverses the hash and its subhashes, executing the given block on the key and value.
h = { "A"=>"A", "B"=>"B" }
h.traverse! { |k,v| [k.downcase, v] }
h #=> { "a"=>"A", "b"=>"B" }
CREDIT: Trans
# File lib/core/facets/hash/recursive.rb, line 144
144: def graph!(&block)
145: @enum.replace(graph(&block))
146: end
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", { "X"=>"X" } }
g = h.recursive_map{ |k,v| [k.downcase, v] }
g #=> [["a", "A"], ["b", "B"], [["x", "X"]]]
CREDIT: Trans
# File lib/core/facets/hash/recursive.rb, line 89
89: def map(&block)
90: @enum.inject([]) do |a,(k,v)|
91: if Hash === v
92: v = v.recursive.map(&block)
93: elsif v.respond_to?(:to_hash)
94: v = v.to_hash.recursive.map(&block)
95: end
96: nk, nv = block.call(k,v)
97: a << [nk, nv]
98: a
99: end
100: end
In-place rendition of #.
# File lib/core/facets/hash/recursive.rb, line 104
104: def map!(&b)
105: @enum.replace(map(&b))
106: end
Same as Hash#merge but recursively merges sub-hashes.
# File lib/core/facets/hash/recursive.rb, line 150
150: def merge(other)
151: hash = @enum.dup
152: other.each do |key, value|
153: myval = @enum[key]
154: if value.is_a?(Hash) && myval.is_a?(Hash)
155: hash[key] = myval.recursive.merge(value)
156: else
157: hash[key] = value
158: end
159: end
160: hash
161: end
Same as Hash#merge! but recursively merges sub-hashes.
# File lib/core/facets/hash/recursive.rb, line 165
165: def merge!(other)
166: other.each do |key, value|
167: myval = @enum[key]
168: if value.is_a?(Hash) && myval.is_a?(Hash)
169: myval.recursive.merge!(value)
170: else
171: @enum[key] = value
172: end
173: end
174: @enum
175: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.