From ac18d0bbda6ef4d4927c1b8c35088f17d4d535b3 Mon Sep 17 00:00:00 2001 From: bogdanvlviv Date: Sat, 26 Sep 2020 20:16:22 +0300 Subject: [PATCH] `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. --- activesupport/lib/active_support/core_ext/hash/slice.rb | 5 +++-- activesupport/test/core_ext/hash_ext_test.rb | 2 ++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/hash/slice.rb b/activesupport/lib/active_support/core_ext/hash/slice.rb index 3d0f8a1e62..56bc5de382 100644 --- a/activesupport/lib/active_support/core_ext/hash/slice.rb +++ b/activesupport/lib/active_support/core_ext/hash/slice.rb @@ -18,8 +18,9 @@ class Hash # 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} - # { a: 1, b: 2 }.extract!(:a, :x) # => {:a=>1} + # hash = { a: 1, b: 2, c: 3, d: 4 } + # hash.extract!(:a, :b) # => {:a=>1, :b=>2} + # hash # => {:c=>3, :d=>4} def extract!(*keys) keys.each_with_object(self.class.new) { |key, result| result[key] = delete(key) if has_key?(key) } end diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb index fb45769c19..554ace0b0f 100644 --- a/activesupport/test/core_ext/hash_ext_test.rb +++ b/activesupport/test/core_ext/hash_ext_test.rb @@ -388,11 +388,13 @@ class HashExtTest < ActiveSupport::TestCase def test_extract_nils original = { a: nil, b: nil } expected = { a: nil } + remaining = { b: nil } extracted = original.extract!(:a, :x) assert_equal expected, extracted assert_nil extracted[:a] assert_nil extracted[:x] + assert_equal remaining, original end def test_except