Class: Hash

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

Class Method Summary collapse

Class Method Details

.deep_merge(first, second) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/soar_xt/soar_extend_hash.rb', line 2

def self.deep_merge(first, second)
  # result = first
  if first.is_a?(Hash) and (second.is_a?(Hash))
    result = first.merge(second)
#     elsif first.is_a?(Array) and second.is_a?(Array)
#       return first + second
#     else
#       return second
  end
  first.each do |first_key, first_value|
    second.each do |second_key, second_value|
      if (first_key == second_key)
        if first_value.is_a?(Hash) and (second_value.is_a?(Hash))
          result[first_key] = Hash::deep_merge(first_value, second_value)
        elsif first_value.is_a?(Array) and (second_value.is_a?(Array))
          result[first_key] = first_value + second_value
        else
          puts "tbd"
#             result[first_key] = second_value
        end        
      end
    end
  end     
  result
end