Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: Array#include? and Enumerable#include? should take non-Elem types #2273

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/array.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -2182,7 +2182,7 @@ class Array[unchecked out Elem] < Object
#
# Related: see [Methods for Querying](rdoc-ref:Array@Methods+for+Querying).
#
def include?: (Elem object) -> bool
def include?: (untyped object) -> bool
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initially, I thought to allow taking nil value. Therefore I changed this type as follows once:

Suggested change
def include?: (untyped object) -> bool
def include?: (Elem? object) -> bool
| (untyped object) -> false

But I noticed it does not help the numbers' case. Therefore, I used untyped here finally.


# <!-- rdoc-file=array.c -->
# Returns the zero-based integer index of a specified element, or `nil`.
Expand Down
2 changes: 1 addition & 1 deletion core/enumerable.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
# {foo: 0, bar: 1, baz: 2}.include?('foo') # => false
# {foo: 0, bar: 1, baz: 2}.include?(0) # => false
#
def include?: (Elem arg0) -> bool
def include?: (untyped arg0) -> bool

# <!--
# rdoc-file=enum.c
Expand Down
2 changes: 2 additions & 0 deletions test/stdlib/Array_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,8 @@ def test_flatten!
def test_include?
assert_send_type "(Integer) -> bool",
[1,2,3], :include?, 1
assert_send_type "(Float) -> bool",
[1,2,3], :include?, 1.0
end

def test_index
Expand Down
7 changes: 7 additions & 0 deletions test/stdlib/Enumerable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,13 @@ def test_grepv
TestEnumerable.new, :grep_v, '0' do 0 end
end

def test_include?
assert_send_type "(String) -> bool",
TestEnumerable.new, :include?, ''
assert_send_type "(Integer) -> bool",
TestEnumerable.new, :include?, 1
end

def test_inject
assert_send_type "(String init, Symbol method) -> untyped", TestEnumerable.new, :inject, +'', :<<
assert_send_type "(Symbol method) -> String", TestEnumerable.new, :inject, :+
Expand Down
Loading