Class: Hash Private
- 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
Instance Method Summary collapse
-
#assert_valid_keys(*valid_keys) ⇒ void
private
Validates all keys in a hash match
*valid_keys
, raisingArgumentError
on a mismatch. -
#compact_blank ⇒ Hash{Hash::K => Hash::V}
private
Hash#reject has its own definition, so this needs one too.
-
#deep_dup ⇒ T.self_type
private
Returns a deep copy of hash.
-
#deep_merge(other_hash, &block) ⇒ Hash{Hash::K, T.type_parameter(:k2) => T.untyped}
private
Returns a new hash with +self+ and +other_hash+ merged recursively.
-
#deep_merge!(other_hash, &block) ⇒ Hash{Hash::K, T.type_parameter(:k2) => T.untyped}
private
Same as +deep_merge+, but modifies +self+.
-
#deep_stringify_keys ⇒ Hash{String => V}
private
Returns a new hash with all keys converted to strings.
-
#deep_stringify_keys! ⇒ Hash{String => V}
private
Destructively converts all keys to strings.
-
#deep_symbolize_keys ⇒ Hash{Symbol => V}
private
Returns a new hash with all keys converted to symbols, as long as they respond to +to_sym+.
-
#deep_symbolize_keys! ⇒ Hash{Symbol => V}
private
Destructively converts all keys to symbols, as long as they respond to +to_sym+.
-
#deep_transform_keys(&block) ⇒ Hash{T.type_parameter(:out) => V}
private
Returns a new hash with all keys converted by the block operation.
-
#deep_transform_keys!(&block) ⇒ Hash{T.type_parameter(:out) => V}
private
Destructively converts all keys by using the block operation.
-
#deep_transform_values(&block) ⇒ Hash{Hash::K => T.type_parameter(:out)}
private
Returns a new hash with all values converted by the block operation.
-
#deep_transform_values!(&block) ⇒ Hash{Hash::K => T.type_parameter(:out)}
private
Destructively converts all values by using the block operation.
-
#present? ⇒ Boolean
private
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.
{ 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
17 18 19 20 21 22 23 24 25 |
# File 'extend/hash/keys.rb', line 17 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 |
#compact_blank ⇒ Hash{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.
Hash#reject has its own definition, so this needs one too.
29 |
# File 'extend/enumerable.rb', line 29 def compact_blank = reject { |_k, v| T.unsafe(v).blank? } |
#deep_dup ⇒ T.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”
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.
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 } }
19 20 21 |
# File 'extend/hash/deep_merge.rb', line 19 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+.
24 25 26 27 28 29 30 31 32 33 34 |
# File 'extend/hash/deep_merge.rb', line 24 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_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.
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.
hash = { person: { name: 'Rob', age: '28' } }
hash.deep_stringify_keys
# => {"person"=>{"name"=>"Rob", "age"=>"28"}}
50 |
# File 'extend/hash/keys.rb', line 50 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.
55 |
# File 'extend/hash/keys.rb', line 55 def deep_stringify_keys! = T.unsafe(self).deep_transform_keys!(&:to_s) |
#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.
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.
hash = { 'person' => { 'name' => 'Rob', 'age' => '28' } }
hash.deep_symbolize_keys
# => {:person=>{:name=>"Rob", :age=>"28"}}
65 66 67 68 69 70 71 |
# File 'extend/hash/keys.rb', line 65 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.
76 77 78 79 80 81 82 |
# File 'extend/hash/keys.rb', line 76 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.
hash = { person: { name: 'Rob', age: '28' } }
hash.deep_transform_keys{ |key| key.to_s.upcase }
# => {"PERSON"=>{"NAME"=>"Rob", "AGE"=>"28"}}
35 |
# File 'extend/hash/keys.rb', line 35 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.
40 |
# File 'extend/hash/keys.rb', line 40 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.
14 |
# File 'extend/hash/deep_transform_values.rb', line 14 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.
19 |
# File 'extend/hash/deep_transform_values.rb', line 19 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.
100 |
# File 'extend/blank.rb', line 100 def present? = !empty? # :nodoc: |