Skip to content

Commit 79b80b2

Browse files
authored
Merge pull request rails#52424 from Earlopain/drop-hash-except-core-ext
Drop Hash `except` core extension
2 parents def0397 + 8d98a52 commit 79b80b2

File tree

3 files changed

+7
-37
lines changed

3 files changed

+7
-37
lines changed

activesupport/lib/active_support/core_ext/hash/except.rb

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
11
# frozen_string_literal: true
22

33
class Hash
4-
# Returns a hash that includes everything except given keys.
5-
# hash = { a: true, b: false, c: nil }
6-
# hash.except(:c) # => { a: true, b: false }
7-
# hash.except(:a, :b) # => { c: nil }
8-
# hash # => { a: true, b: false, c: nil }
9-
#
10-
# This is useful for limiting a set of parameters to everything but a few known toggles:
11-
# @person.update(params[:person].except(:admin))
12-
def except(*keys)
13-
slice(*self.keys - keys)
14-
end unless method_defined?(:except)
15-
164
# Removes the given keys from hash and returns it.
175
# hash = { a: true, b: false, c: nil }
186
# hash.except!(:c) # => { a: true, b: false }

activesupport/test/core_ext/hash_ext_test.rb

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ def test_methods
4646
assert_respond_to h, :deep_stringify_keys!
4747
assert_respond_to h, :to_options
4848
assert_respond_to h, :to_options!
49-
assert_respond_to h, :except
5049
assert_respond_to h, :except!
5150
end
5251

@@ -401,10 +400,6 @@ def test_except
401400
original = { a: "x", b: "y", c: 10 }
402401
expected = { a: "x", b: "y" }
403402

404-
# Should return a new hash without the given keys.
405-
assert_equal expected, original.except(:c)
406-
assert_not_equal expected, original
407-
408403
# Should replace the hash without the given keys.
409404
assert_equal expected, original.except!(:c)
410405
assert_equal expected, original
@@ -414,26 +409,15 @@ def test_except_with_more_than_one_argument
414409
original = { a: "x", b: "y", c: 10 }
415410
expected = { a: "x" }
416411

417-
assert_equal expected, original.except(:b, :c)
418-
419412
assert_equal expected, original.except!(:b, :c)
420413
assert_equal expected, original
421414
end
422415

423416
def test_except_with_original_frozen
424417
original = { a: "x", b: "y" }
425418
original.freeze
426-
assert_nothing_raised { original.except(:a) }
427-
428419
assert_raise(FrozenError) { original.except!(:a) }
429420
end
430-
431-
def test_except_does_not_delete_values_in_original
432-
original = { a: "x", b: "y" }
433-
assert_not_called(original, :delete) do
434-
original.except(:a)
435-
end
436-
end
437421
end
438422

439423
class IWriteMyOwnXML

guides/source/active_support_core_extensions.md

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2881,27 +2881,25 @@ NOTE: Defined in `active_support/core_ext/object/deep_dup.rb`.
28812881

28822882
### Working with Keys
28832883

2884-
#### `except` and `except!`
2884+
#### `except!`
28852885

2886-
The method [`except`][Hash#except] returns a hash with the keys in the argument list removed, if present:
2886+
The method [`except!`][Hash#except!] is identical to the built-in `except` method but removes keys in place, returning `self`.
28872887

28882888
```ruby
2889-
{ a: 1, b: 2 }.except(:a) # => {:b=>2}
2889+
{ a: 1, b: 2 }.except!(:a) # => {:b=>2}
2890+
{ a: 1, b: 2 }.except!(:c) # => {:a=>1, :b=>2}
28902891
```
28912892

2892-
If the receiver responds to `convert_key`, the method is called on each of the arguments. This allows `except` to play nice with hashes with indifferent access for instance:
2893+
If the receiver responds to `convert_key`, the method is called on each of the arguments. This allows `except!` (and `except`) to play nice with hashes with indifferent access for instance:
28932894

28942895
```ruby
2895-
{ a: 1 }.with_indifferent_access.except(:a) # => {}
2896-
{ a: 1 }.with_indifferent_access.except("a") # => {}
2896+
{ a: 1 }.with_indifferent_access.except!(:a) # => {}
2897+
{ a: 1 }.with_indifferent_access.except!("a") # => {}
28972898
```
28982899

2899-
There's also the bang variant [`except!`][Hash#except!] that removes keys in place.
2900-
29012900
NOTE: Defined in `active_support/core_ext/hash/except.rb`.
29022901

29032902
[Hash#except!]: https://api.rubyonrails.org/classes/Hash.html#method-i-except-21
2904-
[Hash#except]: https://api.rubyonrails.org/classes/Hash.html#method-i-except
29052903

29062904
#### `stringify_keys` and `stringify_keys!`
29072905

0 commit comments

Comments
 (0)