class Hash
def slice!(*keys)
{ a: 1, b: 2, c: 3, d: 4 }.slice!(:a, :b)
Returns a hash containing the removed key/value pairs.
Replaces the hash with only the given keys.
def slice!(*keys) keys.map! { |key| convert_key(key) } if respond_to?(:convert_key, true) omit = slice(*self.keys - keys) hash = slice(*keys) hash.default = default hash.default_proc = default_proc if default_proc replace(hash) omit end