Class: Hash

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

Direct Known Subclasses

Utils::TopologicalHash

Instance Method Summary collapse

Instance Method Details

#assert_valid_keys(*valid_keys) ⇒ void

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

Parameters:

  • valid_keys (T.untyped)


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_blankObject

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_dupT.self_type

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) ⇒ Object

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) ⇒ Object

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_keysObject

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 # => “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!Object

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_keysObject

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 # => :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!Object

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) ⇒ Object

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 } # => “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) ⇒ Object

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) ⇒ Object

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.

hash = { person: { name: ‘Rob’, age: ‘28’ } }

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



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

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

#deep_transform_values!(&block) ⇒ Object

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



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

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

#present?Boolean

Returns:

  • (Boolean)


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

def present? = !empty? # :nodoc: