Module: RuboCop::SortedMethodsByCall::Util

Defined in:
lib/rubocop/sorted_methods_by_call/extensions/util.rb

Overview

:nodoc:

Class Method Summary collapse

Class Method Details

.deep_merge(h, other) ⇒ Hash

RuboCop::SortedMethodsByCall::Util.deep_merge(hash, other) -> Hash

Merges two hashes without overwriting values that share the same key. When a key exists in both hashes, values are accumulated into an array (“buckets”). Scalars are wrapped to arrays automatically.

  • Non-destructive: returns a new Hash; does not mutate hash.

  • If other is not a Hash, the original hash is returned as-is.

Examples:

Accumulate values into buckets

base  = { main: :abc, class_T: :hi }
other = { class_T: :h1 }
RuboCop::SortedMethodsByCall::Util.deep_merge(base, other)
#=> { main: [:abc], class_T: [:hi, :h1] }

Parameters:

  • h (Hash)

    The base hash to merge from.

  • other (Hash)

    The hash to merge into hash.

Returns:

  • (Hash)

    A new hash with accumulated values per key.

See Also:

  • Hash#merge


25
26
27
28
29
# File 'lib/rubocop/sorted_methods_by_call/extensions/util.rb', line 25

def self.deep_merge(h, other)
  return h unless other.is_a?(Hash)

  h.merge(other) { |_, a, b| Array(a) + Array(b) }
end