Class: Hash Private

Inherits:
Object show all
Defined in:
extend/hash/keys.rb,
extend/blank.rb,
extend/enumerable.rb,
extend/hash/deep_merge.rb,
extend/object/deep_dup.rb,
extend/hash/deep_transform_values.rb,
extend/hash/keys.rbi,
extend/enumerable.rbi,
extend/hash/deep_merge.rbi,
extend/hash/deep_transform_values.rbi

This class is part of a private API. This class may only be used in the Homebrew/brew repository. Third parties should avoid using this class if possible, as it may be removed or changed without warning.

Direct Known Subclasses

Utils::TopologicalHash

Instance Method Summary collapse

Instance Method Details

#assert_valid_keys(*valid_keys) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Validates all keys in a hash match *valid_keys, raising ArgumentError on a mismatch.

Note that keys are treated differently than HashWithIndifferentAccess, meaning that string and symbol keys will not match.

Example

{ name: 'Rob', years: '28' }.assert_valid_keys(:name, :age)
# => raises "ArgumentError: Unknown key: :years. Valid keys are: :name, :age"
{ name: 'Rob', age: '28' }.assert_valid_keys('name', 'age')
# => raises "ArgumentError: Unknown key: :name. Valid keys are: 'name', 'age'"
{ name: 'Rob', age: '28' }.assert_valid_keys(:name, :age)   # => passes, raises nothing

Parameters:

  • valid_keys (T.untyped)


21
22
23
24
25
26
27
28
29
# File 'extend/hash/keys.rb', line 21

def assert_valid_keys(*valid_keys)
  valid_keys.flatten!
  each_key do |k|
    next if valid_keys.include?(k)

    raise ArgumentError,
          "Unknown key: #{T.unsafe(k).inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(", ")}"
  end
end

#blank?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

A hash is blank if it's empty:

{}.blank?                # => true
{ key: 'value' }.blank?  # => false

Returns:

  • (Boolean)


114
# File 'extend/blank.rb', line 114

def blank? = empty?

#compact_blankHash{Hash::K => Hash::V}

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

#reject has its own definition, so this needs one too.

Returns:

  • (Hash{Hash::K => Hash::V})


37
# File 'extend/enumerable.rb', line 37

def compact_blank = reject { |_k, v| T.unsafe(v).blank? }

#deep_dupT.self_type

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns a deep copy of hash.

hash = { a: { b: 'b' } } dup = hash.deep_dup dup[:a][:c] = 'c'

hash[:a][:c] # => nil dup[:a][:c] # => "c"

Returns:

  • (T.self_type)


47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'extend/object/deep_dup.rb', line 47

def deep_dup
  hash = dup
  each_pair do |key, value|
    case key
    when ::String, ::Symbol
      hash[key] = T.unsafe(value).deep_dup
    else
      hash.delete(key)
      hash[T.unsafe(key).deep_dup] = T.unsafe(value).deep_dup
    end
  end
  hash
end

#deep_merge(other_hash, &block) ⇒ Hash{Hash::K, T.type_parameter(:k2) => T.untyped}

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns a new hash with self and other_hash merged recursively.

Examples

h1 = { a: true, b: { c: [1, 2, 3] } }
h2 = { a: false, b: { x: [3, 4, 5] } }

h1.deep_merge(h2) # => { a: false, b: { c: [1, 2, 3], x: [3, 4, 5] } }

Like with Hash#merge in the standard library, a block can be provided to merge values:

h1 = { a: 100, b: 200, c: { c1: 100 } }
h2 = { b: 250, c: { c1: 200 } }
h1.deep_merge(h2) { |key, this_val, other_val| this_val + other_val }
# => { a: 100, b: 450, c: { c1: 300 } }

Parameters:

  • other_hash (Hash{T.type_parameter(:k2) => T.untyped})
  • block (T.proc.params(k: T.untyped, v1: T.untyped, v2: T.untyped).returns(T.untyped), nil)

Returns:

  • (Hash{Hash::K, T.type_parameter(:k2) => T.untyped})


25
26
27
# File 'extend/hash/deep_merge.rb', line 25

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

#deep_merge!(other_hash, &block) ⇒ Hash{Hash::K, T.type_parameter(:k2) => T.untyped}

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Same as #deep_merge, but modifies self.

Parameters:

  • other_hash (Hash{T.type_parameter(:k2) => T.untyped})
  • block (T.proc.params(k: T.untyped, v1: T.untyped, v2: T.untyped).returns(T.untyped), nil)

Returns:

  • (Hash{Hash::K, T.type_parameter(:k2) => T.untyped})


30
31
32
33
34
35
36
37
38
39
40
# File 'extend/hash/deep_merge.rb', line 30

def deep_merge!(other_hash, &block)
  merge!(other_hash) do |key, this_val, other_val|
    if T.unsafe(this_val).is_a?(Hash) && other_val.is_a?(Hash)
      T.unsafe(this_val).deep_merge(other_val, &block)
    elsif block
      yield(key, this_val, other_val)
    else
      other_val
    end
  end
end

#deep_stringify_keysHash{String => V}

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns a new hash with all keys converted to strings. This includes the keys from the root hash and from all nested hashes and arrays.

Example

hash = { person: { name: 'Rob', age: '28' } }

hash.deep_stringify_keys
# => {"person"=>{"name"=>"Rob", "age"=>"28"}}

Returns:



62
# File 'extend/hash/keys.rb', line 62

def deep_stringify_keys = T.unsafe(self).deep_transform_keys(&:to_s)

#deep_stringify_keys!Hash{String => V}

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Destructively converts all keys to strings. This includes the keys from the root hash and from all nested hashes and arrays.

Returns:



67
# File 'extend/hash/keys.rb', line 67

def deep_stringify_keys! = T.unsafe(self).deep_transform_keys!(&:to_s)

#deep_symbolize_keysHash{Symbol => V}

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns a new hash with all keys converted to symbols, as long as they respond to to_sym. This includes the keys from the root hash and from all nested hashes and arrays.

Example

hash = { 'person' => { 'name' => 'Rob', 'age' => '28' } }

hash.deep_symbolize_keys
# => {:person=>{:name=>"Rob", :age=>"28"}}

Returns:



81
82
83
84
85
86
87
# File 'extend/hash/keys.rb', line 81

def deep_symbolize_keys
  deep_transform_keys do |key|
    T.unsafe(key).to_sym
  rescue
    key
  end
end

#deep_symbolize_keys!Hash{Symbol => V}

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Destructively converts all keys to symbols, as long as they respond to to_sym. This includes the keys from the root hash and from all nested hashes and arrays.

Returns:



92
93
94
95
96
97
98
# File 'extend/hash/keys.rb', line 92

def deep_symbolize_keys!
  deep_transform_keys! do |key|
    T.unsafe(key).to_sym
  rescue
    key
  end
end

#deep_transform_keys(&block) ⇒ Hash{T.type_parameter(:out) => V}

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns a new hash with all keys converted by the block operation. This includes the keys from the root hash and from all nested hashes and arrays.

Example

hash = { person: { name: 'Rob', age: '28' } }

hash.deep_transform_keys{ |key| key.to_s.upcase }
# => {"PERSON"=>{"NAME"=>"Rob", "AGE"=>"28"}}

Parameters:

  • block (T.proc.params(o: K).returns(T.type_parameter(:out)))

Returns:

  • (Hash{T.type_parameter(:out) => V})


43
# File 'extend/hash/keys.rb', line 43

def deep_transform_keys(&block) = _deep_transform_keys_in_object(self, &block)

#deep_transform_keys!(&block) ⇒ Hash{T.type_parameter(:out) => V}

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Destructively converts all keys by using the block operation. This includes the keys from the root hash and from all nested hashes and arrays.

Parameters:

  • block (T.proc.params(o: K).returns(T.type_parameter(:out)))

Returns:

  • (Hash{T.type_parameter(:out) => V})


48
# File 'extend/hash/keys.rb', line 48

def deep_transform_keys!(&block) = _deep_transform_keys_in_object!(self, &block)

#deep_transform_values(&block) ⇒ Hash{Hash::K => T.type_parameter(:out)}

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns a new hash with all values converted by the block operation. This includes the values from the root hash and from all nested hashes and arrays.

Example

hash = { person: { name: 'Rob', age: '28' } }

hash.deep_transform_values{ |value| value.to_s.upcase }
# => {person: {name: "ROB", age: "28"}}

Parameters:

  • block (T.proc.params(o: Hash::V).returns(T.type_parameter(:out)))

Returns:

  • (Hash{Hash::K => T.type_parameter(:out)})


17
# File 'extend/hash/deep_transform_values.rb', line 17

def deep_transform_values(&block) = _deep_transform_values_in_object(self, &block)

#deep_transform_values!(&block) ⇒ Hash{Hash::K => T.type_parameter(:out)}

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Destructively converts all values by using the block operation. This includes the values from the root hash and from all nested hashes and arrays.

Parameters:

  • block (T.proc.params(o: Hash::V).returns(T.type_parameter(:out)))

Returns:

  • (Hash{Hash::K => T.type_parameter(:out)})


22
# File 'extend/hash/deep_transform_values.rb', line 22

def deep_transform_values!(&block) = _deep_transform_values_in_object!(self, &block)

#present?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


117
# File 'extend/blank.rb', line 117

def present? = !empty? # :nodoc: