class Hash
def extract!(*keys)
hash.extract!(:a, :b) # => {:a=>1, :b=>2}
hash = { a: 1, b: 2, c: 3, d: 4 }
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