Skip to content

Instantly share code, notes, and snippets.

@danielharan
Created October 6, 2008 16:18
Show Gist options
  • Save danielharan/15072 to your computer and use it in GitHub Desktop.
Save danielharan/15072 to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment