Method: Hash#slice!

Defined in:
lib/nutella/core_ext/hash.rb

#slice!(*keys) ⇒ Hash Also known as: grab!

Acts on the hash as described in Hash#slice, but modifies the hash in place.

Examples:

Demonstrates the action and its return value

test = { a: 1, b: 2, c: 3, d: 4 }
test.slice! :a, :c  # => { b: 2, d: 4 }
test                # => { a: 1, c: 3 }

Parameters:

  • keys (*Object)

    the keys to slice out of the hash

Returns:

  • (Hash)

    the removed key/value pairs



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