Skip to content

Commit

Permalink
Merge pull request #7007 from Mik-die/hash_extract
Browse files Browse the repository at this point in the history
make Hash#extract! more symmetric with Hash#slice
  • Loading branch information
rafaelfranca committed Oct 12, 2012
2 parents 3e75369 + a4b1196 commit e68b97a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
11 changes: 11 additions & 0 deletions activesupport/CHANGELOG.md
@@ -1,5 +1,16 @@
## Rails 4.0.0 (unreleased) ## ## 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* * 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: * Tests tag the Rails log with the current test class and test case:
Expand Down
6 changes: 3 additions & 3 deletions activesupport/lib/active_support/core_ext/hash/slice.rb
Expand Up @@ -32,9 +32,9 @@ def slice!(*keys)


# 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, c: 3, d: 4 }.extract!(:a, :b) # => { a: 1, b: 2 }
# # => {:a => 1, :b => 2} # { a: 1, b: 2 }.extract!(:a, :x) # => { a: 1 }
def extract!(*keys) 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
end end
26 changes: 25 additions & 1 deletion activesupport/test/core_ext/hash_ext_test.rb
Expand Up @@ -723,8 +723,32 @@ def test_indifferent_slice_access_with_symbols
def test_extract def test_extract
original = {:a => 1, :b => 2, :c => 3, :d => 4} original = {:a => 1, :b => 2, :c => 3, :d => 4}
expected = {:a => 1, :b => 2} 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 end


def test_except def test_except
Expand Down
12 changes: 10 additions & 2 deletions guides/source/active_support_core_extensions.md
Expand Up @@ -2867,8 +2867,16 @@ The method `extract!` removes and returns the key/value pairs matching the given


```ruby ```ruby
hash = {:a => 1, :b => 2} hash = {:a => 1, :b => 2}
rest = hash.extract!(:a) # => {:a => 1} rest = hash.extract!(:a, :x) # => {:a => 1} # non-existing keys are ignored
hash # => {:b => 2} 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`. NOTE: Defined in `active_support/core_ext/hash/slice.rb`.
Expand Down

0 comments on commit e68b97a

Please sign in to comment.