class Hash
Public Instance Methods
@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 merges self (left) Hash
with another Hash
On keys existing in both Hashes:
-
merges
Array
values by union operator -
for other values overwrites left
Hash
value with the rightHash
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
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
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
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
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
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
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