mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Hash#extract!
: Clarify its docs and assert remaining value in test_extract_nils
This change emphasizes a remaining value after applying `extract!` on a hash.
This commit is contained in:
parent
f40c17dcfa
commit
ac18d0bbda
2 changed files with 5 additions and 2 deletions
|
@ -18,8 +18,9 @@ class Hash
|
||||||
|
|
||||||
# Removes and returns the key/value pairs matching the given keys.
|
# Removes and returns the key/value pairs matching the given keys.
|
||||||
#
|
#
|
||||||
# { a: 1, b: 2, c: 3, d: 4 }.extract!(:a, :b) # => {:a=>1, :b=>2}
|
# hash = { a: 1, b: 2, c: 3, d: 4 }
|
||||||
# { a: 1, b: 2 }.extract!(:a, :x) # => {:a=>1}
|
# hash.extract!(:a, :b) # => {:a=>1, :b=>2}
|
||||||
|
# hash # => {:c=>3, :d=>4}
|
||||||
def extract!(*keys)
|
def extract!(*keys)
|
||||||
keys.each_with_object(self.class.new) { |key, result| result[key] = delete(key) if has_key?(key) }
|
keys.each_with_object(self.class.new) { |key, result| result[key] = delete(key) if has_key?(key) }
|
||||||
end
|
end
|
||||||
|
|
|
@ -388,11 +388,13 @@ class HashExtTest < ActiveSupport::TestCase
|
||||||
def test_extract_nils
|
def test_extract_nils
|
||||||
original = { a: nil, b: nil }
|
original = { a: nil, b: nil }
|
||||||
expected = { a: nil }
|
expected = { a: nil }
|
||||||
|
remaining = { b: nil }
|
||||||
extracted = original.extract!(:a, :x)
|
extracted = original.extract!(:a, :x)
|
||||||
|
|
||||||
assert_equal expected, extracted
|
assert_equal expected, extracted
|
||||||
assert_nil extracted[:a]
|
assert_nil extracted[:a]
|
||||||
assert_nil extracted[:x]
|
assert_nil extracted[:x]
|
||||||
|
assert_equal remaining, original
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_except
|
def test_except
|
||||||
|
|
Loading…
Reference in a new issue