Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/mimi/core/core_ext.rb
Instance Method Summary collapse
-
#deep_dup ⇒ Hash
Duplicates a Hash with all nested values.
-
#deep_merge(right) ⇒ Hash
A new Hash.
-
#deep_merge!(right) ⇒ Hash
Deep merges self (left) Hash with another Hash.
-
#deep_stringify_keys ⇒ Hash
Stringifies Hash keys including all nested Hashes.
-
#deep_stringify_keys! ⇒ Hash
Modifies the Hash stringifying its keys including all nested Hashes.
-
#deep_symbolize_keys ⇒ Hash
Symbolizes Hash keys including all nested Hashes.
-
#deep_symbolize_keys! ⇒ Hash
Modifies the Hash symbolizing its keys including all nested Hashes.
-
#except(*keys) ⇒ Hash
Returns a Hash with given keys excluded, if present.
-
#except!(*keys) ⇒ Hash
Modifies the Hash excluding given keys, if present.
-
#only(*keys) ⇒ Hash
Returns a Hash with only given keys, if present.
-
#only!(*keys) ⇒ Hash
Modifies the Hash keeping only given keys, if present.
-
#stringify_keys ⇒ Hash
Stringifies Hash keys.
-
#stringify_keys! ⇒ Hash
Modifies the Hash stringifying its keys.
-
#symbolize_keys ⇒ Hash
Symbolizes Hash keys.
-
#symbolize_keys! ⇒ Hash
Modifies the Hash symbolizing its keys.
Instance Method Details
#deep_dup ⇒ Hash
Duplicates a Hash with all nested values
120 121 122 123 124 125 |
# 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) ⇒ Hash
Returns a new Hash.
109 110 111 |
# File 'lib/mimi/core/core_ext.rb', line 109 def deep_merge(right) deep_dup.deep_merge!(right) end |
#deep_merge!(right) ⇒ Hash
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
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# 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 ⇒ Hash
Stringifies Hash keys including all nested Hashes
201 202 203 204 205 206 207 |
# 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! ⇒ Hash
Modifies the Hash stringifying its keys including all nested Hashes
213 214 215 |
# File 'lib/mimi/core/core_ext.rb', line 213 def deep_stringify_keys! replace(deep_stringify_keys) end |
#deep_symbolize_keys ⇒ Hash
Symbolizes Hash keys including all nested Hashes
156 157 158 159 160 161 162 |
# 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! ⇒ Hash
Modifies the Hash symbolizing its keys including all nested Hashes
168 169 170 |
# File 'lib/mimi/core/core_ext.rb', line 168 def deep_symbolize_keys! replace(deep_symbolize_keys) end |
#except(*keys) ⇒ Hash
Returns a Hash with given keys excluded, if present
50 51 52 |
# File 'lib/mimi/core/core_ext.rb', line 50 def except(*keys) dup.except!(*keys) end |
#except!(*keys) ⇒ Hash
Modifies the Hash excluding given keys, if present
64 65 66 67 68 69 70 71 |
# 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) ⇒ Hash
Returns a Hash with only given keys, if present
15 16 17 |
# File 'lib/mimi/core/core_ext.rb', line 15 def only(*keys) dup.only!(*keys) end |
#only!(*keys) ⇒ Hash
Modifies the Hash keeping only given keys, if present
29 30 31 32 33 34 35 36 |
# 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 ⇒ Hash
Stringifies Hash keys
179 180 181 182 183 184 |
# 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! ⇒ Hash
Modifies the Hash stringifying its keys
190 191 192 |
# File 'lib/mimi/core/core_ext.rb', line 190 def stringify_keys! replace(stringify_keys) end |
#symbolize_keys ⇒ Hash
Symbolizes Hash keys
134 135 136 137 138 139 |
# 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! ⇒ Hash
Modifies the Hash symbolizing its keys
145 146 147 |
# File 'lib/mimi/core/core_ext.rb', line 145 def symbolize_keys! replace(symbolize_keys) end |