Guest User

Untitled

a guest
Feb 20th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.45 KB | None | 0 0
  1. class Hash
  2. def +(other)
  3. (self.keys + other.keys).uniq.each do |key|
  4. if [self[key], other[key]].all?
  5. if self[key].is_a?(Hash)
  6. self[key] += other[key]
  7. elsif self[key].respond_to?(:+)
  8. self[key] += other[key]
  9. else
  10. raise "values for #{key} were neither hashes or summable"
  11. end
  12. else
  13. self[key] ||= other[key] # ignore the nil
  14. end
  15. end
  16. self
  17. end
  18. end
Add Comment
Please, Sign In to add comment