Class: Hash
Instance Method Summary collapse
-
#slice(*keys) ⇒ Hash
(also: #grab)
Grab the given keys out of a hash.
-
#slice!(*keys) ⇒ Hash
(also: #grab!)
Acts on the hash as described in Hash#slice, but modifies the hash in place.
Instance Method Details
#slice(*keys) ⇒ Hash Also known as: grab
Grab the given keys out of a hash.
20 21 22 |
# File 'lib/nutella/core_ext/hash.rb', line 20 def slice(*keys) select { |k, v| keys.include? k } end |
#slice!(*keys) ⇒ Hash Also known as: grab!
Acts on the hash as described in Hash#slice, but modifies the hash in place.
35 36 37 38 |
# File 'lib/nutella/core_ext/hash.rb', line 35 def slice!(*keys) replace((original = self.dup).slice *keys) original.reject { |k, v| keys.include? k } end |