method
extract!
v6.1.3.1 -
Show latest stable
- Class:
Hash
extract!(*keys)public
Removes and returns the key/value pairs matching the given keys.
hash = { a: 1, b: 2, c: 3, d: 4 } hash.extract!(:a, :b) # => {:a=>1, :b=>2} hash # => {:c=>3, :d=>4}
2Notes
without a bang
We can use hash#slice[http://api.rubyonrails.org/classes/Hash.html#method-i-slice] if we want an Hash#extract (without bang) like behavior.
Non-existent key semantics changed.
For Rails 4.0, the behaviour of this has changed when you pass a key that isn't in the hash.
3.2 (undocumented):
{ a: 1, b: 2 }.extract!(:a, :x) # => {:a=>1, :x => nil}
4.0 (as per docs):
{ a: 1, b: 2 }.extract!(:a, :x) # => {:a=>1}
The 4.0 behaviour is now consistent with the behaviour of slice, the 3.2 behaviour was not.