class Hash
Public Instance Methods
Check equality of hashes, considering if difference between string and symbol keys should be observed. See {Hash#with_indifferent_equality}.
# File lib/tfg/support/core_ext/hash/with_indifferent_equality.rb, line 19 def ==(other) if other.is_a?(TFG::Support::HashWithIndifferentEquality) other == self else old_double_equals(other) end end
Allows chaining of keys through nested hashes.
“`ruby hash = {foo: true, bar: {baz: {qux: false}}}
hash.deep #=> true hash.deep[:bar, :baz, :qux] #=> false “`
When the chain of keys is incomplete `nil` will be returned.
“`ruby hash[:bar, :qux] #=> nil “`
Assignment is also possible, and the key chain will be created if needed.
“`ruby hash = {} hash[:foo, :bar] = true hash #=> {foo: {bar: true}} “`
# File lib/tfg/support/core_ext/hash/deep.rb, line 27 def deep TFG::Support::DeepHashAccessor.new(self) end
Allows you to map the values of a hash. The original keys will be retained.
“`ruby {foo: 1, bar: 2}.map_values {|value| value * 2 } #=> {foo: 2, bar: 4} “`
The original hash is not mutated.
# File lib/tfg/support/core_ext/hash/map_values.rb, line 10 def map_values Hash.new.tap do |result| self.each do |key, value| result[key] = yield(value) end end end
The same as {Hash#map_values}, except mutates and returns the original hash.
“`ruby hash = {foo: 1, bar: 2} hash.map_values {|value| value * 2 } hash #=> {foo: 2, bar: 4} “`
# File lib/tfg/support/core_ext/hash/map_values.rb, line 25 def map_values! self.tap do |hash| hash.each do |key, value| hash[key] = yield(value) end end end
Will return a new hash that does not differentiate between string and symbol keys for equality.
“`ruby {a: 1} == {“a” => 1}.with_indifferent_equality #=> true “`
# File lib/tfg/support/core_ext/hash/with_indifferent_equality.rb, line 9 def with_indifferent_equality TFG::Support::HashWithIndifferentEquality.new(self) end