class Hash

Public Instance Methods

deep_dup() click to toggle source

Duplicates a Hash with all nested values

@return [Hash] a new Hash

# File lib/mimi/core/core_ext.rb, line 120
def deep_dup
  map do |k, v|
    v = v.respond_to?(:deep_dup) ? v.deep_dup : v.dup
    [k, v]
  end.to_h
end
deep_merge(right) click to toggle source

@see deep_merge!

@param right [Hash] the right (other) Hash @return [Hash] a new Hash

# File lib/mimi/core/core_ext.rb, line 109
def deep_merge(right)
  deep_dup.deep_merge!(right)
end
deep_merge!(right) click to toggle source

Deep merges self (left) Hash with another Hash

On keys existing in both Hashes:

  • merges Hash values by merging left Hash with the right Hash

  • merges Array values by union operator

  • for other values overwrites left Hash value with the right Hash value

@param right [Hash] the right (other) Hash @return [Hash] self

# File lib/mimi/core/core_ext.rb, line 86
def deep_merge!(right)
  right.each do |k ,v|
    unless self.key?(k)
      self[k] = v
      next
    end
    if self[k].is_a?(Hash) && v.is_a?(Hash)
      self[k].deep_merge!(v)
    elsif self[k].is_a?(Array) && v.is_a?(Array)
      self[k] = self[k] | v
    else
      # unmergeable values, overwrite
      self[k] = v
    end
  end
  self
end
deep_stringify_keys() click to toggle source

Stringifies Hash keys including all nested Hashes

@return [Hash] a new Hash

# File lib/mimi/core/core_ext.rb, line 201
def deep_stringify_keys
  map do |k, v|
    k = k.respond_to?(:to_s) ? k.to_s : k
    v = v.respond_to?(:deep_stringify_keys) ? v.deep_stringify_keys : v
    [k, v]
  end.to_h
end
deep_stringify_keys!() click to toggle source

Modifies the Hash stringifying its keys including all nested Hashes

@return [Hash] self

# File lib/mimi/core/core_ext.rb, line 213
def deep_stringify_keys!
  replace(deep_stringify_keys)
end
deep_symbolize_keys() click to toggle source

Symbolizes Hash keys including all nested Hashes

@return [Hash] a new Hash

# File lib/mimi/core/core_ext.rb, line 156
def deep_symbolize_keys
  map do |k, v|
    k = k.respond_to?(:to_sym) ? k.to_sym : k
    v = v.respond_to?(:deep_symbolize_keys) ? v.deep_symbolize_keys : v
    [k, v]
  end.to_h
end
deep_symbolize_keys!() click to toggle source

Modifies the Hash symbolizing its keys including all nested Hashes

@return [Hash] self

# File lib/mimi/core/core_ext.rb, line 168
def deep_symbolize_keys!
  replace(deep_symbolize_keys)
end
except(*keys) click to toggle source

Returns a Hash with given keys excluded, if present

@param *keys [*] list of keys @return [Hash] a new Hash

@example

h = { a: 1, b: 2, :c 3 }
h.except(:a, :b, :d) # => { c: 3 }
# File lib/mimi/core/core_ext.rb, line 50
def except(*keys)
  dup.except!(*keys)
end
except!(*keys) click to toggle source

Modifies the Hash excluding given keys, if present

@param *keys [*] list of keys @return [Hash] self

@example

h = { a: 1, b: 2, :c 3 }
h.except!(:a, :b, :d)
h # => { c: 3 }
# File lib/mimi/core/core_ext.rb, line 64
def except!(*keys)
  if keys.size == 1 && keys.first.is_a?(Array)
    raise ArgumentError, 'Hash#except!() expects keys as list of arguments,' \
      ' not an Array as first argument'
  end
  reject! { |k, _| keys.include?(k) }
  self
end
only(*keys) click to toggle source

Returns a Hash with only given keys, if present

@param *keys [*] list of keys @return [Hash] a new Hash

@example

h = { a: 1, b: 2, :c 3 }
h.only(:a, :b, :d) # => { a: 1, b: 2 }
# File lib/mimi/core/core_ext.rb, line 15
def only(*keys)
  dup.only!(*keys)
end
only!(*keys) click to toggle source

Modifies the Hash keeping only given keys, if present

@param *keys [*] list of keys @return [Hash] self

@example

h = { a: 1, b: 2, :c 3 }
h.only!(:a, :b, :d)
h # => { a: 1, b: 2 }
# File lib/mimi/core/core_ext.rb, line 29
def only!(*keys)
  if keys.size == 1 && keys.first.is_a?(Array)
    raise ArgumentError, 'Hash#only!() expects keys as list of arguments,' \
      ' not an Array as first argument'
  end
  select! { |k, _| keys.include?(k) }
  self
end
stringify_keys() click to toggle source

Stringifies Hash keys

@return [Hash] a new Hash

# File lib/mimi/core/core_ext.rb, line 179
def stringify_keys
  map do |k, v|
    k = k.respond_to?(:to_s) ? k.to_s : k
    [k, v]
  end.to_h
end
stringify_keys!() click to toggle source

Modifies the Hash stringifying its keys

@return [Hash] self

# File lib/mimi/core/core_ext.rb, line 190
def stringify_keys!
  replace(stringify_keys)
end
symbolize_keys() click to toggle source

Symbolizes Hash keys

@return [Hash] a new Hash

# File lib/mimi/core/core_ext.rb, line 134
def symbolize_keys
  map do |k, v|
    k = k.respond_to?(:to_sym) ? k.to_sym : k
    [k, v]
  end.to_h
end
symbolize_keys!() click to toggle source

Modifies the Hash symbolizing its keys

@return [Hash] self

# File lib/mimi/core/core_ext.rb, line 145
def symbolize_keys!
  replace(symbolize_keys)
end