Module: HashRecursiveMerge

Included in:
Hash
Defined in:
lib/lib/utils.rb

Instance Method Summary collapse

Instance Method Details

#rmerge(other_hash, concat_if_array = false) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/lib/utils.rb', line 133

def rmerge(other_hash, concat_if_array = false)
  r = {}
  return merge(other_hash) do |key, oldval, newval|
    if oldval.is_a?(Hash)
      r[key] = oldval.rmerge(newval, concat_if_array)
    elsif oldval.is_a?(Array) and newval.is_a?(Array)
      r[key] = concat_if_array ? oldval + newval : newval
    else
      newval
    end
  end
end

#rmerge!(other_hash, concat_if_array = false) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
# File 'lib/lib/utils.rb', line 146

def rmerge!(other_hash, concat_if_array = false)
  return merge!(other_hash) do |key, oldval, newval|
    if oldval.is_a?(Hash)
      oldval.rmerge!(newval, concat_if_array)
    elsif oldval.is_a?(Array) and newval.is_a?(Array)
      concat_if_array ? oldval + newval : newval
    else
      newval
    end
  end
end