Class: XMLMunger::NestedHash
- Inherits:
-
Hash
- Object
- Hash
- XMLMunger::NestedHash
- Defined in:
- lib/xmlmunger/nested_hash.rb
Instance Method Summary collapse
-
#map_values(&block) ⇒ Object
Map terminal hash values in a nested hash to an array > nh = NestedHash[a: { c: 2 }, b: 1] => { a: { c: 2 }, b: 1 } > nh.map_values => [2, 1] > nh.map_values { |x| x + 1 } => [3, 2].
-
#map_values_with_route(route = [], &block) ⇒ Object
Map terminal hash values in a nested hash to an array with route tracking > nh = NestedHash[a: { c: 2 }, b: 1] => { a: { c: 2 }, b: 1 } > nh.map_values_with_route { |route, value| route << value**value } => [[:a, :c, 4], [:b, 1]].
-
#transform(&block) ⇒ Object
Map terminal hash values in a nested hash > nh = NestedHash[a: { c: 2 }, b: 1] => { a: { c: 2 }, b: 1 } > nh.transform { |value| value + 1 } => { a: { c: 3 }, b: 2 }.
-
#transform_with_route(route = [], &block) ⇒ Object
Map terminal hash values in a nested hash with route tracking > nh = NestedHash[a: { c: 2 }, b: 1] => { a: { c: 2 }, b: 1 } > nh.transform_with_route { |route, value| “#-> ’) -> #value” } => { a: { c: “a -> c -> 2” }, b: “b -> 1” }.
Instance Method Details
#map_values(&block) ⇒ Object
Map terminal hash values in a nested hash to an array > nh = NestedHash[a: { c: 2 }, b: 1]
> { a: { c: 2 }, b: 1 }
> nh.map_values
> [2, 1]
> nh.map_values { |x| x + 1 }
> [3, 2]
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/xmlmunger/nested_hash.rb', line 58 def map_values(&block) values = [] self.each { |key, value| values.concat case value.class.to_s when 'Hash', 'NestedHash' then NestedHash[value].map_values(&block) else [block_given? ? yield(value) : value] end } values end |
#map_values_with_route(route = [], &block) ⇒ Object
Map terminal hash values in a nested hash to an array with route tracking > nh = NestedHash[a: { c: 2 }, b: 1]
> { a: { c: 2 }, b: 1 }
> nh.map_values_with_route { |route, value| route << value**value }
> [[:a, :c, 4], [:b, 1]]
76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/xmlmunger/nested_hash.rb', line 76 def map_values_with_route(route = [], &block) values = [] self.each { |key, value| route_copy = route.dup values.concat case value.class.to_s when 'Hash', 'NestedHash' then NestedHash[value].map_values_with_route(route_copy.concat([key]), &block) else [block_given? ? yield(route_copy.concat([key]), value) : route_copy.concat([key, value])] end } values end |
#transform(&block) ⇒ Object
Map terminal hash values in a nested hash > nh = NestedHash[a: { c: 2 }, b: 1]
> { a: { c: 2 }, b: 1 }
> nh.transform { |value| value + 1 }
> { a: { c: 3 }, b: 2 }
22 23 24 25 26 27 28 29 30 |
# File 'lib/xmlmunger/nested_hash.rb', line 22 def transform(&block) # http://stackoverflow.com/questions/5189161/changing-every-value-in-a-hash-in-ruby NestedHash[self.map { |key, value| [key, case value.class.to_s when 'Hash', 'NestedHash' then NestedHash[value].transform(&block) else yield value end] }] end |
#transform_with_route(route = [], &block) ⇒ Object
Map terminal hash values in a nested hash with route tracking > nh = NestedHash[a: { c: 2 }, b: 1]
> { a: { c: 2 }, b: 1 }
> nh.transform_with_route { |route, value| “#-> ’) -> #value” }
> { a: { c: “a -> c -> 2” }, b: “b -> 1” }
39 40 41 42 43 44 45 46 47 |
# File 'lib/xmlmunger/nested_hash.rb', line 39 def transform_with_route(route = [], &block) NestedHash[self.map { |key, value| [key, case value.class.to_s when 'Hash', 'NestedHash' then NestedHash[value].transform_with_route(route.dup.concat([key]), &block) else yield route.dup.concat([key]), value end] }] end |