Class: Hash Private
- 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.
Direct Known Subclasses
Instance Method Summary collapse
-
#blank? ⇒ Boolean
private
A hash is blank if it's empty:.
-
#compact_blank ⇒ Hash{Hash::K => Hash::V}
private
#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
selfandother_hashmerged 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_symbolize_keys ⇒ Hash{Symbol, K => V}
private
Returns a new hash with all keys converted 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.
- #present? ⇒ Boolean private
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
13 |
# File 'extend/blank/hash.rb', line 13 def blank? = empty? |
#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.
#reject has its own definition, so this needs one too.
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_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"
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 } }
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.
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_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.
Example
hash = { person: { name: 'Rob', age: '28' } }
hash.deep_stringify_keys
# => {"person"=>{"name"=>"Rob", "age"=>"28"}}
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_keys ⇒ Hash{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"}}
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"}}
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.
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"}}
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.
16 |
# File 'extend/blank/hash.rb', line 16 def present? = !empty? # :nodoc: |