Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/hash_ext.rb

Instance Method Summary collapse

Instance Method Details

#+(other) ⇒ Object

Recursively adds hashes

> {:a => 1, :b => 1} + {:a => 1}
=> {:a => 2, :b => 1}


5
6
7
8
9
10
11
12
13
14
# File 'lib/hash_ext.rb', line 5

def +(other)
  (self.keys + other.keys).uniq.each do |key|
    if [self[key], other[key]].all?
      self[key] += other[key]
    else
      self[key] ||= other[key] # ignore the nil
    end
  end
  self
end