Skip to content

Commit 43b78d2

Browse files
committed
core: Array#include? and Enumerable#include? should take non-Elem types
For example, `Array[Integer]` can take other number types validly: ``` > [1, 2, 3].include? 2.0 => true > [1, 2, 3].include? Complex(2) => true > [1, 2, 3].include? Rational(2) => true ``` refs: soutaro/steep#1482
1 parent a7178b1 commit 43b78d2

File tree

4 files changed

+11
-2
lines changed

4 files changed

+11
-2
lines changed

core/array.rbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2182,7 +2182,7 @@ class Array[unchecked out Elem] < Object
21822182
#
21832183
# Related: see [Methods for Querying](rdoc-ref:Array@Methods+for+Querying).
21842184
#
2185-
def include?: (Elem object) -> bool
2185+
def include?: (untyped object) -> bool
21862186

21872187
# <!-- rdoc-file=array.c -->
21882188
# Returns the zero-based integer index of a specified element, or `nil`.

core/enumerable.rbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
718718
# {foo: 0, bar: 1, baz: 2}.include?('foo') # => false
719719
# {foo: 0, bar: 1, baz: 2}.include?(0) # => false
720720
#
721-
def include?: (Elem arg0) -> bool
721+
def include?: (untyped arg0) -> bool
722722

723723
# <!--
724724
# rdoc-file=enum.c

test/stdlib/Array_test.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,8 @@ def test_flatten!
461461
def test_include?
462462
assert_send_type "(Integer) -> bool",
463463
[1,2,3], :include?, 1
464+
assert_send_type "(Float) -> bool",
465+
[1,2,3], :include?, 1.0
464466
end
465467

466468
def test_index

test/stdlib/Enumerable_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,13 @@ def test_grepv
224224
TestEnumerable.new, :grep_v, '0' do 0 end
225225
end
226226

227+
def test_include?
228+
assert_send_type "(String) -> bool",
229+
TestEnumerable.new, :include?, ''
230+
assert_send_type "(Integer) -> bool",
231+
TestEnumerable.new, :include?, 1
232+
end
233+
227234
def test_inject
228235
assert_send_type "(String init, Symbol method) -> untyped", TestEnumerable.new, :inject, +'', :<<
229236
assert_send_type "(Symbol method) -> String", TestEnumerable.new, :inject, :+

0 commit comments

Comments
 (0)