Skip to content

Commit d7da403

Browse files
committed
Revert "Object#in? also accepts multiple parameters"
This reverts commit ebf69ab. `in?` must not take multiple parameters because its behavior would be ambiguous: # Test if "B" is included in a list of names with `"B".in?(*names)`: names = ["BMorearty"] "B".in?(*names) # => true names = ["BMorearty","rubyduo"] "B".in?(*names) # => false Conflicts: activesupport/lib/active_support/core_ext/object/inclusion.rb activesupport/test/core_ext/object/inclusion_test.rb
1 parent 7ead1d8 commit d7da403

File tree

2 files changed

+9
-29
lines changed

2 files changed

+9
-29
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])

0 commit comments

Comments
 (0)