class Hash

Public Instance Methods

==(other) click to toggle source

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
Also aliased as: old_double_equals
deep() click to toggle source

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
map_values() { |value| ... } click to toggle source

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
map_values!() { |value| ... } click to toggle source

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
old_double_equals(other)

Aliased to original {Hash#==}.

Alias for: ==
with_indifferent_equality() click to toggle source

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