class Hash

def extract!(*keys)

{ a: 1, b: 2 }.extract!(:a, :x) # => {:a=>1}
{ a: 1, b: 2, c: 3, d: 4 }.extract!(:a, :b) # => {:a=>1, :b=>2}

Removes and returns the key/value pairs matching the given keys.
def extract!(*keys)
  keys.each_with_object(self.class.new) { |key, result| result[key] = delete(key) if has_key?(key) }
end