diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2012-10-12 14:29:11 -0700 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2012-10-12 14:29:11 -0700 |
commit | e68b97a4b65787711e50aa1565b4dd0077a00d6d (patch) | |
tree | 229a43700512aed4a8713c09afac293622951f89 | |
parent | 3e75369b7263a0ec09c45dd1371b1641797e9604 (diff) | |
parent | a4b11961630febd556c962b788b11c0ed5bedb45 (diff) | |
download | rails-e68b97a4b65787711e50aa1565b4dd0077a00d6d.tar.gz rails-e68b97a4b65787711e50aa1565b4dd0077a00d6d.tar.bz2 rails-e68b97a4b65787711e50aa1565b4dd0077a00d6d.zip |
Merge pull request #7007 from Mik-die/hash_extract
make Hash#extract! more symmetric with Hash#slice
-rw-r--r-- | activesupport/CHANGELOG.md | 11 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/hash/slice.rb | 6 | ||||
-rw-r--r-- | activesupport/test/core_ext/hash_ext_test.rb | 26 | ||||
-rw-r--r-- | guides/source/active_support_core_extensions.md | 12 |
4 files changed, 49 insertions, 6 deletions
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 0a12ba6cdd..23e2ce0b03 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,5 +1,16 @@ ## Rails 4.0.0 (unreleased) ## +* Hash#extract! returns only those keys that present in the receiver. + + {:a => 1, :b => 2}.extract!(:a, :x) # => {:a => 1} + + *Mikhail Dieterle* + +* Hash#extract! returns the same subclass, that the receiver is. I.e. + HashWithIndifferentAccess#extract! returns HashWithIndifferentAccess instance. + + *Mikhail Dieterle* + * Optimize ActiveSupport::Cache::Entry to reduce memory and processing overhead. *Brian Durand* * Tests tag the Rails log with the current test class and test case: diff --git a/activesupport/lib/active_support/core_ext/hash/slice.rb b/activesupport/lib/active_support/core_ext/hash/slice.rb index 45fec57009..de09f26f3d 100644 --- a/activesupport/lib/active_support/core_ext/hash/slice.rb +++ b/activesupport/lib/active_support/core_ext/hash/slice.rb @@ -32,9 +32,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, c: 3, d: 4 }.extract!(:a, :b) # => { a: 1, b: 2 } + # { a: 1, b: 2 }.extract!(:a, :x) # => { a: 1 } def extract!(*keys) - keys.each_with_object({}) { |key, result| result[key] = delete(key) } + keys.each_with_object(self.class.new) { |key, result| result[key] = delete(key) if has_key?(key) } end end diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb index 53ea2aad16..b208767490 100644 --- a/activesupport/test/core_ext/hash_ext_test.rb +++ b/activesupport/test/core_ext/hash_ext_test.rb @@ -723,8 +723,32 @@ class HashExtTest < ActiveSupport::TestCase def test_extract original = {:a => 1, :b => 2, :c => 3, :d => 4} expected = {:a => 1, :b => 2} + remaining = {:c => 3, :d => 4} - assert_equal expected, original.extract!(:a, :b) + assert_equal expected, original.extract!(:a, :b, :x) + assert_equal remaining, original + end + + def test_extract_nils + original = {:a => nil, :b => nil} + expected = {:a => nil} + extracted = original.extract!(:a, :x) + + assert_equal expected, extracted + assert_equal nil, extracted[:a] + assert_equal nil, extracted[:x] + end + + def test_indifferent_extract + original = {:a => 1, 'b' => 2, :c => 3, 'd' => 4}.with_indifferent_access + expected = {:a => 1, :b => 2}.with_indifferent_access + remaining = {:c => 3, :d => 4}.with_indifferent_access + + [['a', 'b'], [:a, :b]].each do |keys| + copy = original.dup + assert_equal expected, copy.extract!(*keys) + assert_equal remaining, copy + end end def test_except diff --git a/guides/source/active_support_core_extensions.md b/guides/source/active_support_core_extensions.md index 645498437d..f990b4f79f 100644 --- a/guides/source/active_support_core_extensions.md +++ b/guides/source/active_support_core_extensions.md @@ -2867,8 +2867,16 @@ The method `extract!` removes and returns the key/value pairs matching the given ```ruby hash = {:a => 1, :b => 2} -rest = hash.extract!(:a) # => {:a => 1} -hash # => {:b => 2} +rest = hash.extract!(:a, :x) # => {:a => 1} # non-existing keys are ignored +hash # => {:b => 2} +``` + +The method `extract!` returns the same subclass of Hash, that the receiver is. + +```ruby +hash = {:a => 1, :b => 2}.with_indifferent_access +rest = hash.extract!(:a).class +# => ActiveSupport::HashWithIndifferentAccess ``` NOTE: Defined in `active_support/core_ext/hash/slice.rb`. |