Class: Hash Private

Inherits:
Object show all
Defined in:
extend/hash/keys.rb,
extend/blank/hash.rb,
extend/enumerable/hash.rb,
extend/hash/deep_merge.rb,
extend/object/deep_dup/hash.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.

Instance Method Summary collapse

Instance Method Details

#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)


13
# File 'extend/blank/hash.rb', line 13

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})


6
7
8
9
10
11
12
# File 'extend/enumerable/hash.rb', line 6

def compact_blank
  reject do |_k, v|
    case v
    when Object then v.blank?
    end
  end
end

#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)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'extend/object/deep_dup/hash.rb', line 14

def deep_dup
  hash = dup
  each_pair do |key, value|
    duplicated_value = case value
    when ::Object then value.deep_dup
    else value
    end

    case key
    when ::String, ::Symbol
      hash[key] = duplicated_value
    when ::Object
      hash.delete(key)
      hash[key.deep_dup] = duplicated_value
    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
41
42
43
# File 'extend/hash/deep_merge.rb', line 30

def deep_merge!(other_hash, &block)
  merge!(other_hash) do |key, this_val, other_val|
    case this_val
    when Hash
      next this_val.deep_merge(other_val, &block) if other_val.is_a?(Hash)
    end

    if 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:



36
37
38
39
40
41
42
43
# File 'extend/hash/keys.rb', line 36

def deep_stringify_keys
  deep_transform_keys do |key|
    case key
    when Object then key.to_s
    else raise TypeError, "Hash keys must be Objects"
    end
  end
end

#deep_symbolize_keysHash{Symbol, K => 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:



57
58
59
60
61
62
63
64
65
66
67
68
# File 'extend/hash/keys.rb', line 57

def deep_symbolize_keys
  deep_transform_keys do |key|
    case key
    when Object
      # `to_sym` is duck-typed rather than defined on `Object`.
      key.respond_to?(:to_sym) ? key.public_send(:to_sym) : key # rubocop:disable Style/SendWithLiteralMethodName
    else key
    end
  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})


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

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})


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

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)

#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)


16
# File 'extend/blank/hash.rb', line 16

def present? = !empty? # :nodoc: