Skip to content

Commit f7f8b7c

Browse files
committed
Merge pull request rails#10394 from BMorearty/remove-varargs-from-in
Remove varargs from `Object#in?`
2 parents 630d2e4 + 62c62bc commit f7f8b7c

File tree

3 files changed

+10
-31
lines changed

3 files changed

+10
-31
lines changed
Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,15 @@
11
class Object
2-
# Returns true if this object is included in the argument(s). Argument must be
3-
# any object which responds to +#include?+ or optionally, multiple arguments can be passed in. Usage:
2+
# Returns true if this object is included in the argument. Argument must be
3+
# any object which responds to +#include?+. Usage:
44
#
5-
# characters = ['Konata', 'Kagami', 'Tsukasa']
6-
# 'Konata'.in?(characters) # => true
5+
# characters = ["Konata", "Kagami", "Tsukasa"]
6+
# "Konata".in?(characters) # => true
77
#
8-
# character = 'Konata'
9-
# character.in?('Konata', 'Kagami', 'Tsukasa') # => true
10-
#
11-
# This will throw an ArgumentError if a single argument is passed in and it doesn't respond
8+
# This will throw an ArgumentError if the argument doesn't respond
129
# to +#include?+.
13-
def in?(*args)
14-
if args.length > 1
15-
args.include? self
16-
else
17-
another_object = args.first
18-
if another_object.respond_to? :include?
19-
another_object.include? self
20-
else
21-
raise ArgumentError.new 'The single parameter passed to #in? must respond to #include?'
22-
end
23-
end
10+
def in?(another_object)
11+
another_object.include?(self)
12+
rescue NoMethodError
13+
raise ArgumentError.new("The parameter passed to #in? must respond to #include?")
2414
end
2515
end

activesupport/test/core_ext/object/inclusion_test.rb

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,6 @@
22
require 'active_support/core_ext/object/inclusion'
33

44
class InTest < ActiveSupport::TestCase
5-
def test_in_multiple_args
6-
assert :b.in?(:a,:b)
7-
assert !:c.in?(:a,:b)
8-
end
9-
10-
def test_in_multiple_arrays
11-
assert [1,2].in?([1,2],[2,3])
12-
assert ![1,2].in?([1,3],[2,1])
13-
end
14-
155
def test_in_array
166
assert 1.in?([1,2])
177
assert !3.in?([1,2])

guides/source/active_support_core_extensions.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,12 +476,11 @@ NOTE: Defined in `active_support/core_ext/kernel/reporting.rb`.
476476

477477
### `in?`
478478

479-
The predicate `in?` tests if an object is included in another object or a list of objects. An `ArgumentError` exception will be raised if a single argument is passed and it does not respond to `include?`.
479+
The predicate `in?` tests if an object is included in another object. An `ArgumentError` exception will be raised if the argument passed does not respond to `include?`.
480480

481481
Examples of `in?`:
482482

483483
```ruby
484-
1.in?(1,2) # => true
485484
1.in?([1,2]) # => true
486485
"lo".in?("hello") # => true
487486
25.in?(30..50) # => false

0 commit comments

Comments
 (0)