Class: Hash

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

Instance Method Summary collapse

Instance Method Details

#deep_merge(other_hash, &block) ⇒ Object



334
335
336
# File 'lib/api_mini_tester/test_step.rb', line 334

def deep_merge(other_hash, &block)
  dup.deep_merge!(other_hash, &block)
end

#deep_merge!(other_hash, &block) ⇒ Object



338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/api_mini_tester/test_step.rb', line 338

def deep_merge!(other_hash, &block)
  other_hash.each_pair do |current_key, other_value|
    this_value = self[current_key]

    self[current_key] = begin
      if   this_value.is_a?(Hash) && other_value.is_a?(Hash)
        this_value.deep_merge(other_value, &block)
      elsif block_given? && key?(current_key)
        block.call(current_key, this_value, other_value) # rubocop:disable Performance/RedundantBlockCall
      else
        other_value
      end
    end
  end

  self
end