Skip to content

Commit cfb8ed6

Browse files
committed
Add type for Enumerator::Chain
1 parent 4a3ac04 commit cfb8ed6

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

core/enumerator.rbs

+22-2
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,22 @@ end
613613
# This type of objects can be created by Enumerable#chain and Enumerator#+.
614614
#
615615
class Enumerator::Chain[out Elem] < Enumerator[Elem, void]
616-
include Enumerable[Elem]
616+
# * `Rubocop: `self` type is not allowed in this context
617+
# * {Enumerator::Chain#each} without block doesn't return `self`, unlike {Enumerator#each}.
618+
#include Enumerator::_Each[Enum, self] # Workaround: def each:
619+
620+
# <!--
621+
# rdoc-file=enumerator.c
622+
# - Enumerator::Chain.new(*enums) -> enum
623+
# -->
624+
# Generates a new enumerator object that iterates over the elements of given
625+
# enumerable objects in sequence.
626+
#
627+
# e = Enumerator::Chain.new(1..3, [4, 5])
628+
# e.to_a #=> [1, 2, 3, 4, 5]
629+
# e.size #=> 5
630+
#
631+
def initialize: (*_Each[Elem] enums) -> void
617632

618633
# <!--
619634
# rdoc-file=enumerator.c
@@ -626,5 +641,10 @@ class Enumerator::Chain[out Elem] < Enumerator[Elem, void]
626641
#
627642
# If no block is given, returns an enumerator.
628643
#
629-
def each: () { (Elem) -> void } -> void
644+
def each: () { (Enum) -> void } -> self
645+
| () -> Enumerator[E, self]
646+
647+
# wrong argument type chain (expected enumerator) (TypeError)
648+
def with_index: (?Integer) ?{ (?) -> untyped } -> bot
649+
def each_with_index: () ?{ (?) -> untyped } -> bot
630650
end

test/stdlib/Enumerator_test.rb

+19
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,22 @@ def test_to_proc
8888
end.next
8989
end
9090
end
91+
92+
class EnumeratorChainTest < Test::Unit::TestCase
93+
include TestHelper
94+
95+
testing "::Enumerator::Chain[::Integer]"
96+
97+
def test_class_new
98+
assert_send_type "(*_Each[Integer] enums) -> Enumerator::Chain[Integer]",
99+
Enumerator::Chain, :new, 1..3, [4, 5]
100+
end
101+
102+
def test_each
103+
enum = Enumerator::Chain.new 1..3, [4, 5]
104+
assert_send_type "() { (Integer) -> nil } -> Enumerator::Chain[Integer]",
105+
enum, :each do end
106+
assert_send_type "() -> Enumerator[Integer, Enumerator::Chain[Integer]]",
107+
enum, :each
108+
end
109+
end

0 commit comments

Comments
 (0)