Skip to content

Commit eb2f558

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

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

core/enumerator.rbs

+14-9
Original file line numberDiff line numberDiff line change
@@ -612,19 +612,24 @@ end
612612
#
613613
# This type of objects can be created by Enumerable#chain and Enumerator#+.
614614
#
615-
class Enumerator::Chain[out Elem] < Enumerator[Elem, void]
616-
include Enumerable[Elem]
615+
class Enumerator::Chain[out Elem] < Enumerator[Elem, void] # Rubocop: `self` type is not allowed in this context
616+
# {Enumerator::Chain#each} without block doesn't return `self`, unlike {Enumerator#each}
617+
include Enumerator::_Each[Enum, self]
617618

618619
# <!--
619620
# rdoc-file=enumerator.c
620-
# - obj.each(*args) { |...| ... } -> obj
621-
# - obj.each(*args) -> enumerator
621+
# - Enumerator::Chain.new(*enums) -> enum
622622
# -->
623-
# Iterates over the elements of the first enumerable by calling the "each"
624-
# method on it with the given arguments, then proceeds to the following
625-
# enumerables in sequence until all of the enumerables are exhausted.
623+
# Generates a new enumerator object that iterates over the elements of given
624+
# enumerable objects in sequence.
626625
#
627-
# If no block is given, returns an enumerator.
626+
# e = Enumerator::Chain.new(1..3, [4, 5])
627+
# e.to_a #=> [1, 2, 3, 4, 5]
628+
# e.size #=> 5
628629
#
629-
def each: () { (Elem) -> void } -> void
630+
def initialize: (*_Each[Elem] enums) -> void
631+
632+
# wrong argument type chain (expected enumerator) (TypeError)
633+
def with_index: (?Integer) ?{ (?) -> untyped } -> bot
634+
def each_with_index: () ?{ (?) -> untyped } -> bot
630635
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)