Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/madvertise/ext/hash.rb
Overview
Various Hash extensions.
Direct Known Subclasses
Instance Method Summary collapse
-
#deep_merge(other_hash) ⇒ Object
Recursively merge
other_hash
intoself
and return the new hash. -
#deep_merge!(other_hash) ⇒ Object
Recursively merge and replace
other_hash
intoself
. -
#delta_merge!(other_hash) ⇒ Object
accumulate existing keys from
other_hash
intoself
.
Instance Method Details
#deep_merge(other_hash) ⇒ Object
Recursively merge other_hash
into self
and return the new hash.
7 8 9 10 11 12 13 |
# File 'lib/madvertise/ext/hash.rb', line 7 def deep_merge(other_hash) self.merge(other_hash) do |key, oldval, newval| oldval = oldval.to_hash if oldval.respond_to?(:to_hash) newval = newval.to_hash if newval.respond_to?(:to_hash) oldval.is_a?(Hash) && newval.is_a?(Hash) ? oldval.deep_merge(newval) : newval end end |
#deep_merge!(other_hash) ⇒ Object
Recursively merge and replace other_hash
into self
.
16 17 18 |
# File 'lib/madvertise/ext/hash.rb', line 16 def deep_merge!(other_hash) replace(deep_merge(other_hash)) end |
#delta_merge!(other_hash) ⇒ Object
accumulate existing keys from other_hash
into self
.
21 22 23 24 25 26 27 28 29 |
# File 'lib/madvertise/ext/hash.rb', line 21 def delta_merge!(other_hash) other_hash.each do |k,v| if self.has_key?(k) self[k] += v else self[k] = v end end end |