Class: HashMerger

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

Overview

This class will deep merge two hashes to create one new hash with all keys and values from both. If there are duplicate keys the latter one wins. If some common keys point to arrays, those arrays will be merged as well. This is different from Hash#deep_merge, and the reason for having this class.

Note that this class uses recursion, which might be dangerous in Ruby due to lack of tail call elimination. Recursion is however the easiest way to do stuff like this.

Instance Method Summary collapse

Constructor Details

#initialize(start) ⇒ HashMerger

Returns a new instance of HashMerger.



12
13
14
# File 'lib/hash_merger.rb', line 12

def initialize(start)
  @start = start
end

Instance Method Details

#merge_with(new) ⇒ Object



16
17
18
# File 'lib/hash_merger.rb', line 16

def merge_with(new)
  do_merge(new, @start.dup)
end