danielharan (owner)

Revisions

gist: 15072 Download_button fork
public
Public Clone URL: git://gist.github.com/15072.git
Embed All Files: show embed
monkey_hash.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Hash
  def +(other)
    (self.keys + other.keys).uniq.each do |key|
      if [self[key], other[key]].all?
        if self[key].is_a?(Hash)
          self[key] += other[key]
        elsif self[key].respond_to?(:+)
          self[key] += other[key]
        else
          raise "values for #{key} were neither hashes or summable"
        end
      else
        self[key] ||= other[key] # ignore the nil
      end
    end
    self
  end
end