Class: Grog::ListHash

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

Class Method Summary collapse

Class Method Details

.merge_lists(h1, h2) ⇒ Object

Merges hashes that have lists as values: when merging identical keys the lists are added

Hash.merge_lists(=> [1, 2], => [3]) => {:a => [1, 2, 3] }



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/grog/list_hash.rb', line 7

def self.merge_lists(h1, h2)
  merged_hash = h1.dup
  h2.keys.each do |key|
    if merged_hash.has_key?(key)
      merged_hash[key] += h2[key]
    else
      merged_hash[key] = h2[key]
    end
  end
  merged_hash
end