Skip to content

Commit 200b36e

Browse files
committed
Support pattern argument for Enumerable#{all,any,none,one}?
1 parent 80af7c2 commit 200b36e

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

core/enumerable.rbs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,11 @@
192192
# find exactly one of each Hash entry.
193193
#
194194
module Enumerable[unchecked out Elem] : _Each[Elem]
195+
%a{private}
196+
interface _Pattern
197+
def ===: (untyped) -> bool
198+
end
199+
195200
# <!--
196201
# rdoc-file=enum.c
197202
# - all? -> true or false
@@ -234,6 +239,7 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
234239
# Related: #any?, #none? #one?.
235240
#
236241
def all?: () -> bool
242+
| (_Pattern) -> bool
237243
| () { (Elem) -> boolish } -> bool
238244

239245
# <!--
@@ -277,6 +283,7 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
277283
# Related: #all?, #none?, #one?.
278284
#
279285
def any?: () -> bool
286+
| (_Pattern) -> bool
280287
| () { (Elem) -> boolish } -> bool
281288

282289
# <!--
@@ -1167,6 +1174,7 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
11671174
# Related: #one?, #all?, #any?.
11681175
#
11691176
def none?: () -> bool
1177+
| (_Pattern) -> bool
11701178
| () { (Elem) -> boolish } -> bool
11711179

11721180
# <!--
@@ -1210,6 +1218,7 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
12101218
# Related: #none?, #all?, #any?.
12111219
#
12121220
def one?: () -> bool
1221+
| (_Pattern) -> bool
12131222
| () { (Elem) -> boolish } -> bool
12141223

12151224
# <!--

test/stdlib/Enumerable_test.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,4 +241,28 @@ def test_first
241241
assert_send_type '(ToInt n) -> ::Array[::String]' , TestEnumerable.new, :first, ToInt.new(42)
242242
assert_send_type '(ToInt n) -> ::Array[::String]' , TestEmptyEnumerable.new, :first, ToInt.new(42)
243243
end
244+
245+
def test_all_p
246+
assert_send_type '() -> bool', TestEnumerable.new, :all?
247+
assert_send_type '(Class) -> bool', TestEnumerable.new, :all?, String
248+
assert_send_type '() { (String) -> bool } -> bool', TestEnumerable.new, :all? do |x| x == '0' end
249+
end
250+
251+
def test_any_p
252+
assert_send_type '() -> bool', TestEnumerable.new, :any?
253+
assert_send_type '(Class) -> bool', TestEnumerable.new, :any?, String
254+
assert_send_type '() { (String) -> bool } -> bool', TestEnumerable.new, :any? do |x| x == '0' end
255+
end
256+
257+
def test_none_p
258+
assert_send_type '() -> bool', TestEnumerable.new, :none?
259+
assert_send_type '(Class) -> bool', TestEnumerable.new, :none?, String
260+
assert_send_type '() { (String) -> bool } -> bool', TestEnumerable.new, :none? do |x| x == '0' end
261+
end
262+
263+
def test_one_p
264+
assert_send_type '() -> bool', TestEnumerable.new, :one?
265+
assert_send_type '(Class) -> bool', TestEnumerable.new, :one?, String
266+
assert_send_type '() { (String) -> bool } -> bool', TestEnumerable.new, :one? do |x| x == '0' end
267+
end
244268
end

0 commit comments

Comments
 (0)