rbs 2.0.0 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +5 -0
- data/CHANGELOG.md +23 -0
- data/README.md +6 -1
- data/core/array.rbs +2866 -1086
- data/core/basic_object.rbs +150 -30
- data/core/binding.rbs +33 -0
- data/core/builtin.rbs +4 -4
- data/core/class.rbs +43 -5
- data/core/comparable.rbs +57 -0
- data/core/complex.rbs +170 -4
- data/core/constants.rbs +51 -0
- data/core/deprecated.rbs +7 -0
- data/core/dir.rbs +305 -20
- data/core/encoding.rbs +472 -77
- data/core/enumerable.rbs +2173 -234
- data/core/enumerator.rbs +448 -182
- data/core/env.rbs +448 -1
- data/core/errno.rbs +1 -10
- data/core/errors.rbs +152 -2
- data/core/exception.rbs +201 -127
- data/core/false_class.rbs +27 -0
- data/core/fiber.rbs +118 -37
- data/core/fiber_error.rbs +8 -9
- data/core/file.rbs +1059 -139
- data/core/file_test.rbs +287 -32
- data/core/float.rbs +776 -300
- data/core/gc.rbs +185 -34
- data/core/global_variables.rbs +5 -1
- data/core/hash.rbs +1582 -649
- data/core/integer.rbs +974 -204
- data/core/io/buffer.rbs +710 -0
- data/core/io/wait.rbs +29 -8
- data/core/io.rbs +2438 -417
- data/core/kernel.rbs +2315 -316
- data/core/marshal.rbs +37 -2
- data/core/match_data.rbs +123 -6
- data/core/math.rbs +126 -6
- data/core/method.rbs +226 -102
- data/core/module.rbs +421 -45
- data/core/nil_class.rbs +64 -0
- data/core/numeric.rbs +620 -142
- data/core/object.rbs +453 -81
- data/core/object_space.rbs +92 -2
- data/core/proc.rbs +482 -285
- data/core/process.rbs +443 -34
- data/core/ractor.rbs +232 -9
- data/core/random.rbs +151 -52
- data/core/range.rbs +885 -160
- data/core/rational.rbs +122 -6
- data/core/rb_config.rbs +14 -4
- data/core/refinement.rbs +44 -0
- data/core/regexp.rbs +156 -14
- data/core/ruby_vm.rbs +42 -3
- data/core/signal.rbs +78 -39
- data/core/string.rbs +2123 -567
- data/core/string_io.rbs +204 -0
- data/core/struct.rbs +283 -28
- data/core/symbol.rbs +304 -30
- data/core/thread.rbs +1288 -688
- data/core/thread_group.rbs +66 -10
- data/core/time.rbs +643 -217
- data/core/trace_point.rbs +100 -12
- data/core/true_class.rbs +24 -0
- data/core/unbound_method.rbs +73 -7
- data/core/warning.rbs +37 -12
- data/docs/CONTRIBUTING.md +40 -34
- data/docs/stdlib.md +3 -102
- data/lib/rbs/annotate/annotations.rb +197 -0
- data/lib/rbs/annotate/formatter.rb +80 -0
- data/lib/rbs/annotate/rdoc_annotator.rb +398 -0
- data/lib/rbs/annotate/rdoc_source.rb +120 -0
- data/lib/rbs/annotate.rb +6 -0
- data/lib/rbs/cli.rb +45 -1
- data/lib/rbs/definition_builder.rb +5 -1
- data/lib/rbs/location_aux.rb +12 -0
- data/lib/rbs/prototype/rb.rb +12 -0
- data/lib/rbs/version.rb +1 -1
- data/sig/annotate/annotations.rbs +102 -0
- data/sig/annotate/formatter.rbs +24 -0
- data/sig/annotate/rdoc_annotater.rbs +82 -0
- data/sig/annotate/rdoc_source.rbs +30 -0
- data/sig/cli.rbs +2 -0
- data/sig/collection/{collections.rbs → sources.rbs} +0 -0
- data/sig/location.rbs +6 -0
- data/sig/method_types.rbs +5 -1
- data/sig/polyfill.rbs +78 -0
- data/stdlib/abbrev/0/abbrev.rbs +6 -0
- data/stdlib/abbrev/0/array.rbs +26 -0
- data/stdlib/base64/0/base64.rbs +31 -0
- data/stdlib/benchmark/0/benchmark.rbs +74 -3
- data/stdlib/bigdecimal/0/big_decimal.rbs +614 -165
- data/stdlib/bigdecimal-math/0/big_math.rbs +41 -64
- data/stdlib/cgi/0/core.rbs +59 -0
- data/stdlib/coverage/0/coverage.rbs +164 -2
- data/stdlib/csv/0/csv.rbs +2862 -398
- data/stdlib/date/0/date.rbs +483 -25
- data/stdlib/date/0/date_time.rbs +187 -12
- data/stdlib/dbm/0/dbm.rbs +152 -17
- data/stdlib/digest/0/digest.rbs +146 -0
- data/stdlib/erb/0/erb.rbs +65 -245
- data/stdlib/fiber/0/fiber.rbs +73 -91
- data/stdlib/fileutils/0/fileutils.rbs +301 -1
- data/stdlib/find/0/find.rbs +9 -0
- data/stdlib/forwardable/0/forwardable.rbs +65 -1
- data/stdlib/io-console/0/io-console.rbs +227 -15
- data/stdlib/ipaddr/0/ipaddr.rbs +161 -0
- data/stdlib/json/0/json.rbs +1146 -144
- data/stdlib/logger/0/formatter.rbs +24 -0
- data/stdlib/logger/0/log_device.rbs +64 -0
- data/stdlib/logger/0/logger.rbs +165 -13
- data/stdlib/logger/0/period.rbs +10 -0
- data/stdlib/logger/0/severity.rbs +26 -0
- data/stdlib/monitor/0/monitor.rbs +163 -0
- data/stdlib/mutex_m/0/mutex_m.rbs +35 -6
- data/stdlib/net-http/0/net-http.rbs +1492 -683
- data/stdlib/nkf/0/nkf.rbs +372 -0
- data/stdlib/objspace/0/objspace.rbs +149 -90
- data/stdlib/openssl/0/openssl.rbs +8108 -71
- data/stdlib/optparse/0/optparse.rbs +487 -19
- data/stdlib/pathname/0/pathname.rbs +425 -124
- data/stdlib/prettyprint/0/prettyprint.rbs +120 -99
- data/stdlib/prime/0/integer-extension.rbs +20 -2
- data/stdlib/prime/0/prime.rbs +88 -21
- data/stdlib/pstore/0/pstore.rbs +102 -0
- data/stdlib/pty/0/pty.rbs +64 -14
- data/stdlib/resolv/0/resolv.rbs +420 -31
- data/stdlib/rubygems/0/basic_specification.rbs +4 -1
- data/stdlib/rubygems/0/config_file.rbs +33 -1
- data/stdlib/rubygems/0/dependency_installer.rbs +4 -3
- data/stdlib/rubygems/0/installer.rbs +13 -1
- data/stdlib/rubygems/0/path_support.rbs +4 -1
- data/stdlib/rubygems/0/platform.rbs +5 -1
- data/stdlib/rubygems/0/request_set.rbs +44 -2
- data/stdlib/rubygems/0/requirement.rbs +65 -2
- data/stdlib/rubygems/0/rubygems.rbs +407 -0
- data/stdlib/rubygems/0/source_list.rbs +13 -0
- data/stdlib/rubygems/0/specification.rbs +21 -1
- data/stdlib/rubygems/0/stream_ui.rbs +3 -1
- data/stdlib/rubygems/0/uninstaller.rbs +8 -1
- data/stdlib/rubygems/0/version.rbs +60 -157
- data/stdlib/securerandom/0/securerandom.rbs +44 -0
- data/stdlib/set/0/set.rbs +420 -106
- data/stdlib/shellwords/0/shellwords.rbs +55 -77
- data/stdlib/singleton/0/singleton.rbs +20 -0
- data/stdlib/socket/0/addrinfo.rbs +210 -9
- data/stdlib/socket/0/basic_socket.rbs +103 -11
- data/stdlib/socket/0/ip_socket.rbs +31 -9
- data/stdlib/socket/0/socket.rbs +586 -38
- data/stdlib/socket/0/tcp_server.rbs +22 -2
- data/stdlib/socket/0/tcp_socket.rbs +12 -1
- data/stdlib/socket/0/udp_socket.rbs +25 -2
- data/stdlib/socket/0/unix_server.rbs +22 -2
- data/stdlib/socket/0/unix_socket.rbs +45 -5
- data/stdlib/strscan/0/string_scanner.rbs +210 -9
- data/stdlib/tempfile/0/tempfile.rbs +58 -10
- data/stdlib/time/0/time.rbs +208 -116
- data/stdlib/timeout/0/timeout.rbs +10 -0
- data/stdlib/tmpdir/0/tmpdir.rbs +13 -4
- data/stdlib/tsort/0/cyclic.rbs +1 -0
- data/stdlib/tsort/0/interfaces.rbs +1 -0
- data/stdlib/tsort/0/tsort.rbs +42 -0
- data/stdlib/uri/0/common.rbs +57 -8
- data/stdlib/uri/0/file.rbs +55 -109
- data/stdlib/uri/0/ftp.rbs +6 -3
- data/stdlib/uri/0/generic.rbs +556 -327
- data/stdlib/uri/0/http.rbs +26 -115
- data/stdlib/uri/0/https.rbs +8 -102
- data/stdlib/uri/0/ldap.rbs +143 -137
- data/stdlib/uri/0/ldaps.rbs +8 -102
- data/stdlib/uri/0/mailto.rbs +3 -0
- data/stdlib/uri/0/rfc2396_parser.rbs +66 -26
- data/stdlib/uri/0/ws.rbs +6 -3
- data/stdlib/uri/0/wss.rbs +5 -3
- data/stdlib/yaml/0/dbm.rbs +151 -87
- data/stdlib/yaml/0/store.rbs +6 -0
- data/stdlib/zlib/0/zlib.rbs +90 -31
- metadata +17 -5
- data/lib/rbs/location.rb +0 -221
data/core/enumerator.rbs
CHANGED
@@ -1,267 +1,533 @@
|
|
1
|
+
# <!-- rdoc-file=enumerator.c -->
|
1
2
|
# A class which allows both internal and external iteration.
|
2
3
|
#
|
3
|
-
# An
|
4
|
-
#
|
4
|
+
# An Enumerator can be created by the following methods.
|
5
|
+
# * Object#to_enum
|
6
|
+
# * Object#enum_for
|
7
|
+
# * Enumerator.new
|
5
8
|
#
|
6
|
-
# - Kernel\#to\_enum
|
7
9
|
#
|
8
|
-
#
|
10
|
+
# Most methods have two forms: a block form where the contents are evaluated for
|
11
|
+
# each item in the enumeration, and a non-block form which returns a new
|
12
|
+
# Enumerator wrapping the iteration.
|
9
13
|
#
|
10
|
-
#
|
14
|
+
# enumerator = %w(one two three).each
|
15
|
+
# puts enumerator.class # => Enumerator
|
11
16
|
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
#
|
15
|
-
# iteration.
|
17
|
+
# enumerator.each_with_object("foo") do |item, obj|
|
18
|
+
# puts "#{obj}: #{item}"
|
19
|
+
# end
|
16
20
|
#
|
17
|
-
#
|
18
|
-
#
|
19
|
-
#
|
21
|
+
# # foo: one
|
22
|
+
# # foo: two
|
23
|
+
# # foo: three
|
20
24
|
#
|
21
|
-
# enumerator.each_with_object("foo")
|
22
|
-
#
|
23
|
-
# end
|
25
|
+
# enum_with_obj = enumerator.each_with_object("foo")
|
26
|
+
# puts enum_with_obj.class # => Enumerator
|
24
27
|
#
|
25
|
-
#
|
26
|
-
# #
|
27
|
-
#
|
28
|
+
# enum_with_obj.each do |item, obj|
|
29
|
+
# puts "#{obj}: #{item}"
|
30
|
+
# end
|
28
31
|
#
|
29
|
-
#
|
30
|
-
#
|
32
|
+
# # foo: one
|
33
|
+
# # foo: two
|
34
|
+
# # foo: three
|
31
35
|
#
|
32
|
-
#
|
33
|
-
#
|
34
|
-
#
|
36
|
+
# This allows you to chain Enumerators together. For example, you can map a
|
37
|
+
# list's elements to strings containing the index and the element as a string
|
38
|
+
# via:
|
35
39
|
#
|
36
|
-
# #
|
37
|
-
#
|
38
|
-
# # foo: three
|
39
|
-
# ```
|
40
|
+
# puts %w[foo bar baz].map.with_index { |w, i| "#{i}:#{w}" }
|
41
|
+
# # => ["0:foo", "1:bar", "2:baz"]
|
40
42
|
#
|
41
|
-
#
|
42
|
-
#
|
43
|
-
#
|
43
|
+
# An Enumerator can also be used as an external iterator. For example,
|
44
|
+
# Enumerator#next returns the next value of the iterator or raises StopIteration
|
45
|
+
# if the Enumerator is at the end.
|
44
46
|
#
|
45
|
-
#
|
46
|
-
#
|
47
|
-
# # =>
|
48
|
-
#
|
47
|
+
# e = [1,2,3].each # returns an enumerator object.
|
48
|
+
# puts e.next # => 1
|
49
|
+
# puts e.next # => 2
|
50
|
+
# puts e.next # => 3
|
51
|
+
# puts e.next # raises StopIteration
|
49
52
|
#
|
50
|
-
#
|
51
|
-
# external
|
52
|
-
#
|
53
|
-
# value of the iterator or raises
|
54
|
-
# [StopIteration](https://ruby-doc.org/core-2.6.3/StopIteration.html) if
|
55
|
-
# the [Enumerator](Enumerator) is at the end.
|
53
|
+
# Note that enumeration sequence by `next`, `next_values`, `peek` and
|
54
|
+
# `peek_values` do not affect other non-external enumeration methods, unless the
|
55
|
+
# underlying iteration method itself has side-effect, e.g. IO#each_line.
|
56
56
|
#
|
57
|
-
#
|
58
|
-
#
|
59
|
-
# puts e.next # => 1
|
60
|
-
# puts e.next # => 2
|
61
|
-
# puts e.next # => 3
|
62
|
-
# puts e.next # raises StopIteration
|
63
|
-
# ```
|
57
|
+
# Moreover, implementation typically uses fibers so performance could be slower
|
58
|
+
# and exception stacktraces different than expected.
|
64
59
|
#
|
65
60
|
# You can use this to implement an internal iterator as follows:
|
66
61
|
#
|
67
|
-
#
|
68
|
-
#
|
69
|
-
#
|
70
|
-
#
|
71
|
-
#
|
72
|
-
#
|
73
|
-
#
|
62
|
+
# def ext_each(e)
|
63
|
+
# while true
|
64
|
+
# begin
|
65
|
+
# vs = e.next_values
|
66
|
+
# rescue StopIteration
|
67
|
+
# return $!.result
|
68
|
+
# end
|
69
|
+
# y = yield(*vs)
|
70
|
+
# e.feed y
|
71
|
+
# end
|
72
|
+
# end
|
73
|
+
#
|
74
|
+
# o = Object.new
|
75
|
+
#
|
76
|
+
# def o.each
|
77
|
+
# puts yield
|
78
|
+
# puts yield(1)
|
79
|
+
# puts yield(1, 2)
|
80
|
+
# 3
|
74
81
|
# end
|
75
|
-
#
|
76
|
-
#
|
77
|
-
#
|
78
|
-
#
|
79
|
-
#
|
80
|
-
# o
|
81
|
-
#
|
82
|
-
#
|
83
|
-
#
|
84
|
-
#
|
85
|
-
# puts yield(1, 2)
|
86
|
-
# 3
|
87
|
-
# end
|
88
|
-
#
|
89
|
-
# # use o.each as an internal iterator directly.
|
90
|
-
# puts o.each {|*x| puts x; [:b, *x] }
|
91
|
-
# # => [], [:b], [1], [:b, 1], [1, 2], [:b, 1, 2], 3
|
92
|
-
#
|
93
|
-
# # convert o.each to an external iterator for
|
94
|
-
# # implementing an internal iterator.
|
95
|
-
# puts ext_each(o.to_enum) {|*x| puts x; [:b, *x] }
|
96
|
-
# # => [], [:b], [1], [:b, 1], [1, 2], [:b, 1, 2], 3
|
97
|
-
# ```
|
82
|
+
#
|
83
|
+
# # use o.each as an internal iterator directly.
|
84
|
+
# puts o.each {|*x| puts x; [:b, *x] }
|
85
|
+
# # => [], [:b], [1], [:b, 1], [1, 2], [:b, 1, 2], 3
|
86
|
+
#
|
87
|
+
# # convert o.each to an external iterator for
|
88
|
+
# # implementing an internal iterator.
|
89
|
+
# puts ext_each(o.to_enum) {|*x| puts x; [:b, *x] }
|
90
|
+
# # => [], [:b], [1], [:b, 1], [1, 2], [:b, 1, 2], 3
|
91
|
+
#
|
98
92
|
class Enumerator[unchecked out Elem, out Return] < Object
|
99
93
|
include Enumerable[Elem]
|
100
94
|
|
95
|
+
# <!--
|
96
|
+
# rdoc-file=enumerator.c
|
97
|
+
# - enum.each { |elm| block } -> obj
|
98
|
+
# - enum.each -> enum
|
99
|
+
# - enum.each(*appending_args) { |elm| block } -> obj
|
100
|
+
# - enum.each(*appending_args) -> an_enumerator
|
101
|
+
# -->
|
102
|
+
# Iterates over the block according to how this Enumerator was constructed. If
|
103
|
+
# no block and no arguments are given, returns self.
|
104
|
+
#
|
105
|
+
# ### Examples
|
106
|
+
#
|
107
|
+
# "Hello, world!".scan(/\w+/) #=> ["Hello", "world"]
|
108
|
+
# "Hello, world!".to_enum(:scan, /\w+/).to_a #=> ["Hello", "world"]
|
109
|
+
# "Hello, world!".to_enum(:scan).each(/\w+/).to_a #=> ["Hello", "world"]
|
110
|
+
#
|
111
|
+
# obj = Object.new
|
112
|
+
#
|
113
|
+
# def obj.each_arg(a, b=:b, *rest)
|
114
|
+
# yield a
|
115
|
+
# yield b
|
116
|
+
# yield rest
|
117
|
+
# :method_returned
|
118
|
+
# end
|
119
|
+
#
|
120
|
+
# enum = obj.to_enum :each_arg, :a, :x
|
121
|
+
#
|
122
|
+
# enum.each.to_a #=> [:a, :x, []]
|
123
|
+
# enum.each.equal?(enum) #=> true
|
124
|
+
# enum.each { |elm| elm } #=> :method_returned
|
125
|
+
#
|
126
|
+
# enum.each(:y, :z).to_a #=> [:a, :x, [:y, :z]]
|
127
|
+
# enum.each(:y, :z).equal?(enum) #=> false
|
128
|
+
# enum.each(:y, :z) { |elm| elm } #=> :method_returned
|
129
|
+
#
|
101
130
|
def each: () { (Elem arg0) -> untyped } -> Return
|
102
131
|
| () -> self
|
103
132
|
|
133
|
+
# <!--
|
134
|
+
# rdoc-file=enumerator.c
|
135
|
+
# - e.feed obj -> nil
|
136
|
+
# -->
|
137
|
+
# Sets the value to be returned by the next yield inside `e`.
|
138
|
+
#
|
139
|
+
# If the value is not set, the yield returns nil.
|
140
|
+
#
|
141
|
+
# This value is cleared after being yielded.
|
142
|
+
#
|
143
|
+
# # Array#map passes the array's elements to "yield" and collects the
|
144
|
+
# # results of "yield" as an array.
|
145
|
+
# # Following example shows that "next" returns the passed elements and
|
146
|
+
# # values passed to "feed" are collected as an array which can be
|
147
|
+
# # obtained by StopIteration#result.
|
148
|
+
# e = [1,2,3].map
|
149
|
+
# p e.next #=> 1
|
150
|
+
# e.feed "a"
|
151
|
+
# p e.next #=> 2
|
152
|
+
# e.feed "b"
|
153
|
+
# p e.next #=> 3
|
154
|
+
# e.feed "c"
|
155
|
+
# begin
|
156
|
+
# e.next
|
157
|
+
# rescue StopIteration
|
158
|
+
# p $!.result #=> ["a", "b", "c"]
|
159
|
+
# end
|
160
|
+
#
|
161
|
+
# o = Object.new
|
162
|
+
# def o.each
|
163
|
+
# x = yield # (2) blocks
|
164
|
+
# p x # (5) => "foo"
|
165
|
+
# x = yield # (6) blocks
|
166
|
+
# p x # (8) => nil
|
167
|
+
# x = yield # (9) blocks
|
168
|
+
# p x # not reached w/o another e.next
|
169
|
+
# end
|
170
|
+
#
|
171
|
+
# e = o.to_enum
|
172
|
+
# e.next # (1)
|
173
|
+
# e.feed "foo" # (3)
|
174
|
+
# e.next # (4)
|
175
|
+
# e.next # (7)
|
176
|
+
# # (10)
|
177
|
+
#
|
104
178
|
def feed: (Elem arg0) -> NilClass
|
105
179
|
|
180
|
+
# <!--
|
181
|
+
# rdoc-file=enumerator.c
|
182
|
+
# - Enumerator.new(size = nil) { |yielder| ... }
|
183
|
+
# -->
|
184
|
+
# Creates a new Enumerator object, which can be used as an Enumerable.
|
185
|
+
#
|
186
|
+
# Iteration is defined by the given block, in which a "yielder" object, given as
|
187
|
+
# block parameter, can be used to yield a value by calling the `yield` method
|
188
|
+
# (aliased as `<<`):
|
189
|
+
#
|
190
|
+
# fib = Enumerator.new do |y|
|
191
|
+
# a = b = 1
|
192
|
+
# loop do
|
193
|
+
# y << a
|
194
|
+
# a, b = b, a + b
|
195
|
+
# end
|
196
|
+
# end
|
197
|
+
#
|
198
|
+
# fib.take(10) # => [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
|
199
|
+
#
|
200
|
+
# The optional parameter can be used to specify how to calculate the size in a
|
201
|
+
# lazy fashion (see Enumerator#size). It can either be a value or a callable
|
202
|
+
# object.
|
203
|
+
#
|
106
204
|
def initialize: (?Integer arg0) { (Enumerator::Yielder arg0) -> void } -> void
|
107
205
|
|
108
|
-
#
|
206
|
+
# <!--
|
207
|
+
# rdoc-file=enumerator.c
|
208
|
+
# - e.inspect -> string
|
209
|
+
# -->
|
210
|
+
# Creates a printable version of *e*.
|
211
|
+
#
|
109
212
|
def inspect: () -> String
|
110
213
|
|
111
|
-
#
|
112
|
-
#
|
113
|
-
#
|
114
|
-
#
|
214
|
+
# <!--
|
215
|
+
# rdoc-file=enumerator.c
|
216
|
+
# - e.next -> object
|
217
|
+
# -->
|
218
|
+
# Returns the next object in the enumerator, and move the internal position
|
219
|
+
# forward. When the position reached at the end, StopIteration is raised.
|
220
|
+
#
|
221
|
+
# ### Example
|
222
|
+
#
|
223
|
+
# a = [1,2,3]
|
224
|
+
# e = a.to_enum
|
225
|
+
# p e.next #=> 1
|
226
|
+
# p e.next #=> 2
|
227
|
+
# p e.next #=> 3
|
228
|
+
# p e.next #raises StopIteration
|
115
229
|
#
|
230
|
+
# See class-level notes about external iterators.
|
116
231
|
#
|
117
|
-
# ```ruby
|
118
|
-
# a = [1,2,3]
|
119
|
-
# e = a.to_enum
|
120
|
-
# p e.next #=> 1
|
121
|
-
# p e.next #=> 2
|
122
|
-
# p e.next #=> 3
|
123
|
-
# p e.next #raises StopIteration
|
124
|
-
# ```
|
125
|
-
#
|
126
|
-
# Note that enumeration sequence by `next` does not affect other
|
127
|
-
# non-external enumeration methods, unless the underlying iteration
|
128
|
-
# methods itself has side-effect, e.g.
|
129
|
-
# [IO\#each\_line](https://ruby-doc.org/core-2.6.3/IO.html#method-i-each_line)
|
130
|
-
# .
|
131
232
|
def next: () -> Elem
|
132
233
|
|
133
|
-
#
|
134
|
-
#
|
135
|
-
#
|
234
|
+
# <!--
|
235
|
+
# rdoc-file=enumerator.c
|
236
|
+
# - e.next_values -> array
|
237
|
+
# -->
|
238
|
+
# Returns the next object as an array in the enumerator, and move the internal
|
239
|
+
# position forward. When the position reached at the end, StopIteration is
|
136
240
|
# raised.
|
137
241
|
#
|
138
|
-
#
|
139
|
-
#
|
140
|
-
#
|
141
|
-
#
|
142
|
-
#
|
143
|
-
#
|
144
|
-
#
|
145
|
-
#
|
146
|
-
#
|
147
|
-
#
|
148
|
-
#
|
149
|
-
#
|
150
|
-
#
|
151
|
-
#
|
152
|
-
#
|
153
|
-
#
|
154
|
-
#
|
155
|
-
#
|
156
|
-
# e
|
157
|
-
#
|
158
|
-
#
|
159
|
-
#
|
160
|
-
#
|
161
|
-
#
|
162
|
-
#
|
163
|
-
#
|
164
|
-
#
|
165
|
-
#
|
166
|
-
#
|
167
|
-
#
|
168
|
-
#
|
169
|
-
#
|
170
|
-
#
|
171
|
-
#
|
172
|
-
# methods unless underlying iteration method itself has side-effect, e.g.
|
173
|
-
# [IO\#each\_line](https://ruby-doc.org/core-2.6.3/IO.html#method-i-each_line)
|
174
|
-
# .
|
242
|
+
# See class-level notes about external iterators.
|
243
|
+
#
|
244
|
+
# This method can be used to distinguish `yield` and `yield nil`.
|
245
|
+
#
|
246
|
+
# ### Example
|
247
|
+
#
|
248
|
+
# o = Object.new
|
249
|
+
# def o.each
|
250
|
+
# yield
|
251
|
+
# yield 1
|
252
|
+
# yield 1, 2
|
253
|
+
# yield nil
|
254
|
+
# yield [1, 2]
|
255
|
+
# end
|
256
|
+
# e = o.to_enum
|
257
|
+
# p e.next_values
|
258
|
+
# p e.next_values
|
259
|
+
# p e.next_values
|
260
|
+
# p e.next_values
|
261
|
+
# p e.next_values
|
262
|
+
# e = o.to_enum
|
263
|
+
# p e.next
|
264
|
+
# p e.next
|
265
|
+
# p e.next
|
266
|
+
# p e.next
|
267
|
+
# p e.next
|
268
|
+
#
|
269
|
+
# ## yield args next_values next
|
270
|
+
# # yield [] nil
|
271
|
+
# # yield 1 [1] 1
|
272
|
+
# # yield 1, 2 [1, 2] [1, 2]
|
273
|
+
# # yield nil [nil] nil
|
274
|
+
# # yield [1, 2] [[1, 2]] [1, 2]
|
275
|
+
#
|
175
276
|
def next_values: () -> ::Array[Elem]
|
176
277
|
|
177
|
-
#
|
178
|
-
#
|
179
|
-
#
|
278
|
+
# <!--
|
279
|
+
# rdoc-file=enumerator.c
|
280
|
+
# - e.peek -> object
|
281
|
+
# -->
|
282
|
+
# Returns the next object in the enumerator, but doesn't move the internal
|
283
|
+
# position forward. If the position is already at the end, StopIteration is
|
180
284
|
# raised.
|
181
285
|
#
|
286
|
+
# See class-level notes about external iterators.
|
287
|
+
#
|
288
|
+
# ### Example
|
289
|
+
#
|
290
|
+
# a = [1,2,3]
|
291
|
+
# e = a.to_enum
|
292
|
+
# p e.next #=> 1
|
293
|
+
# p e.peek #=> 2
|
294
|
+
# p e.peek #=> 2
|
295
|
+
# p e.peek #=> 2
|
296
|
+
# p e.next #=> 2
|
297
|
+
# p e.next #=> 3
|
298
|
+
# p e.peek #raises StopIteration
|
182
299
|
#
|
183
|
-
# ```ruby
|
184
|
-
# a = [1,2,3]
|
185
|
-
# e = a.to_enum
|
186
|
-
# p e.next #=> 1
|
187
|
-
# p e.peek #=> 2
|
188
|
-
# p e.peek #=> 2
|
189
|
-
# p e.peek #=> 2
|
190
|
-
# p e.next #=> 2
|
191
|
-
# p e.next #=> 3
|
192
|
-
# p e.peek #raises StopIteration
|
193
|
-
# ```
|
194
300
|
def peek: () -> Elem
|
195
301
|
|
196
|
-
#
|
197
|
-
#
|
198
|
-
#
|
199
|
-
#
|
200
|
-
#
|
201
|
-
#
|
302
|
+
# <!--
|
303
|
+
# rdoc-file=enumerator.c
|
304
|
+
# - e.peek_values -> array
|
305
|
+
# -->
|
306
|
+
# Returns the next object as an array, similar to Enumerator#next_values, but
|
307
|
+
# doesn't move the internal position forward. If the position is already at the
|
308
|
+
# end, StopIteration is raised.
|
309
|
+
#
|
310
|
+
# See class-level notes about external iterators.
|
311
|
+
#
|
312
|
+
# ### Example
|
202
313
|
#
|
314
|
+
# o = Object.new
|
315
|
+
# def o.each
|
316
|
+
# yield
|
317
|
+
# yield 1
|
318
|
+
# yield 1, 2
|
319
|
+
# end
|
320
|
+
# e = o.to_enum
|
321
|
+
# p e.peek_values #=> []
|
322
|
+
# e.next
|
323
|
+
# p e.peek_values #=> [1]
|
324
|
+
# p e.peek_values #=> [1]
|
325
|
+
# e.next
|
326
|
+
# p e.peek_values #=> [1, 2]
|
327
|
+
# e.next
|
328
|
+
# p e.peek_values # raises StopIteration
|
203
329
|
#
|
204
|
-
# ```ruby
|
205
|
-
# o = Object.new
|
206
|
-
# def o.each
|
207
|
-
# yield
|
208
|
-
# yield 1
|
209
|
-
# yield 1, 2
|
210
|
-
# end
|
211
|
-
# e = o.to_enum
|
212
|
-
# p e.peek_values #=> []
|
213
|
-
# e.next
|
214
|
-
# p e.peek_values #=> [1]
|
215
|
-
# p e.peek_values #=> [1]
|
216
|
-
# e.next
|
217
|
-
# p e.peek_values #=> [1, 2]
|
218
|
-
# e.next
|
219
|
-
# p e.peek_values # raises StopIteration
|
220
|
-
# ```
|
221
330
|
def peek_values: () -> ::Array[Elem]
|
222
331
|
|
332
|
+
# <!--
|
333
|
+
# rdoc-file=enumerator.c
|
334
|
+
# - e.rewind -> e
|
335
|
+
# -->
|
223
336
|
# Rewinds the enumeration sequence to the beginning.
|
224
337
|
#
|
225
|
-
# If the enclosed object responds to a
|
338
|
+
# If the enclosed object responds to a "rewind" method, it is called.
|
339
|
+
#
|
226
340
|
def rewind: () -> self
|
227
341
|
|
228
|
-
#
|
229
|
-
#
|
230
|
-
#
|
231
|
-
#
|
232
|
-
#
|
233
|
-
#
|
234
|
-
#
|
235
|
-
#
|
342
|
+
# <!--
|
343
|
+
# rdoc-file=enumerator.c
|
344
|
+
# - e.size -> int, Float::INFINITY or nil
|
345
|
+
# -->
|
346
|
+
# Returns the size of the enumerator, or `nil` if it can't be calculated lazily.
|
347
|
+
#
|
348
|
+
# (1..100).to_a.permutation(4).size # => 94109400
|
349
|
+
# loop.size # => Float::INFINITY
|
350
|
+
# (1..100).drop_while.size # => nil
|
351
|
+
#
|
236
352
|
def size: () -> (Integer | Float)?
|
237
353
|
|
354
|
+
# <!--
|
355
|
+
# rdoc-file=enumerator.c
|
356
|
+
# - e.with_index(offset = 0) {|(*args), idx| ... }
|
357
|
+
# - e.with_index(offset = 0)
|
358
|
+
# -->
|
359
|
+
# Iterates the given block for each element with an index, which starts from
|
360
|
+
# `offset`. If no block is given, returns a new Enumerator that includes the
|
361
|
+
# index, starting from `offset`
|
362
|
+
#
|
363
|
+
# `offset`
|
364
|
+
# : the starting index to use
|
365
|
+
#
|
238
366
|
def with_index: (?Integer offset) { (Elem arg0, Integer arg1) -> untyped } -> Return
|
239
367
|
| (?Integer offset) -> ::Enumerator[[ Elem, Integer ], Return]
|
240
368
|
|
369
|
+
# <!-- rdoc-file=enumerator.c -->
|
370
|
+
# Iterates the given block for each element with an arbitrary object, `obj`, and
|
371
|
+
# returns `obj`
|
372
|
+
#
|
373
|
+
# If no block is given, returns a new Enumerator.
|
374
|
+
#
|
375
|
+
# ### Example
|
376
|
+
#
|
377
|
+
# to_three = Enumerator.new do |y|
|
378
|
+
# 3.times do |x|
|
379
|
+
# y << x
|
380
|
+
# end
|
381
|
+
# end
|
382
|
+
#
|
383
|
+
# to_three_with_string = to_three.with_object("foo")
|
384
|
+
# to_three_with_string.each do |x,string|
|
385
|
+
# puts "#{string}: #{x}"
|
386
|
+
# end
|
387
|
+
#
|
388
|
+
# # => foo: 0
|
389
|
+
# # => foo: 1
|
390
|
+
# # => foo: 2
|
391
|
+
#
|
241
392
|
def with_object: [U] (U obj) { (Elem, U obj) -> untyped } -> U
|
242
393
|
| [U] (U obj) -> ::Enumerator[[ Elem, U ], U]
|
243
394
|
end
|
244
395
|
|
396
|
+
# <!-- rdoc-file=enumerator.c -->
|
397
|
+
# Generator
|
398
|
+
#
|
245
399
|
class Enumerator::Generator[out Elem] < Object
|
246
400
|
include Enumerable[Elem]
|
247
401
|
|
248
402
|
def each: () { (Elem) -> void } -> void
|
249
403
|
end
|
250
404
|
|
405
|
+
# <!-- rdoc-file=enumerator.c -->
|
406
|
+
# Enumerator::Lazy is a special type of Enumerator, that allows constructing
|
407
|
+
# chains of operations without evaluating them immediately, and evaluating
|
408
|
+
# values on as-needed basis. In order to do so it redefines most of Enumerable
|
409
|
+
# methods so that they just construct another lazy enumerator.
|
410
|
+
#
|
411
|
+
# Enumerator::Lazy can be constructed from any Enumerable with the
|
412
|
+
# Enumerable#lazy method.
|
413
|
+
#
|
414
|
+
# lazy = (1..Float::INFINITY).lazy.select(&:odd?).drop(10).take_while { |i| i < 30 }
|
415
|
+
# # => #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: 1..Infinity>:select>:drop(10)>:take_while>
|
416
|
+
#
|
417
|
+
# The real enumeration is performed when any non-redefined Enumerable method is
|
418
|
+
# called, like Enumerable#first or Enumerable#to_a (the latter is aliased as
|
419
|
+
# #force for more semantic code):
|
420
|
+
#
|
421
|
+
# lazy.first(2)
|
422
|
+
# #=> [21, 23]
|
423
|
+
#
|
424
|
+
# lazy.force
|
425
|
+
# #=> [21, 23, 25, 27, 29]
|
426
|
+
#
|
427
|
+
# Note that most Enumerable methods that could be called with or without a
|
428
|
+
# block, on Enumerator::Lazy will always require a block:
|
429
|
+
#
|
430
|
+
# [1, 2, 3].map #=> #<Enumerator: [1, 2, 3]:map>
|
431
|
+
# [1, 2, 3].lazy.map # ArgumentError: tried to call lazy map without a block
|
432
|
+
#
|
433
|
+
# This class allows idiomatic calculations on long or infinite sequences, as
|
434
|
+
# well as chaining of calculations without constructing intermediate arrays.
|
435
|
+
#
|
436
|
+
# Example for working with a slowly calculated sequence:
|
437
|
+
#
|
438
|
+
# require 'open-uri'
|
439
|
+
#
|
440
|
+
# # This will fetch all URLs before selecting
|
441
|
+
# # necessary data
|
442
|
+
# URLS.map { |u| JSON.parse(URI.open(u).read) }
|
443
|
+
# .select { |data| data.key?('stats') }
|
444
|
+
# .first(5)
|
445
|
+
#
|
446
|
+
# # This will fetch URLs one-by-one, only till
|
447
|
+
# # there is enough data to satisfy the condition
|
448
|
+
# URLS.lazy.map { |u| JSON.parse(URI.open(u).read) }
|
449
|
+
# .select { |data| data.key?('stats') }
|
450
|
+
# .first(5)
|
451
|
+
#
|
452
|
+
# Ending a chain with ".eager" generates a non-lazy enumerator, which is
|
453
|
+
# suitable for returning or passing to another method that expects a normal
|
454
|
+
# enumerator.
|
455
|
+
#
|
456
|
+
# def active_items
|
457
|
+
# groups
|
458
|
+
# .lazy
|
459
|
+
# .flat_map(&:items)
|
460
|
+
# .reject(&:disabled)
|
461
|
+
# .eager
|
462
|
+
# end
|
463
|
+
#
|
464
|
+
# # This works lazily; if a checked item is found, it stops
|
465
|
+
# # iteration and does not look into remaining groups.
|
466
|
+
# first_checked = active_items.find(&:checked)
|
467
|
+
#
|
468
|
+
# # This returns an array of items like a normal enumerator does.
|
469
|
+
# all_checked = active_items.select(&:checked)
|
470
|
+
#
|
251
471
|
class Enumerator::Lazy[out Elem, out Return] < Enumerator[Elem, Return]
|
472
|
+
# <!-- rdoc-file=enumerator.c -->
|
473
|
+
# Expands `lazy` enumerator to an array. See Enumerable#to_a.
|
474
|
+
#
|
252
475
|
alias force to_a
|
476
|
+
|
477
|
+
# <!--
|
478
|
+
# rdoc-file=enumerator.c
|
479
|
+
# - lazy.compact -> lazy_enumerator
|
480
|
+
# -->
|
481
|
+
# Like Enumerable#compact, but chains operation to be lazy-evaluated.
|
482
|
+
#
|
483
|
+
def compact: () -> Enumerator::Lazy[Elem, Return]
|
253
484
|
end
|
254
485
|
|
486
|
+
# <!-- rdoc-file=enumerator.c -->
|
487
|
+
# Yielder
|
488
|
+
#
|
255
489
|
class Enumerator::Yielder < Object
|
256
490
|
def <<: (untyped arg0) -> void
|
257
491
|
|
258
492
|
def yield: (*untyped arg0) -> void
|
259
493
|
|
494
|
+
# <!--
|
495
|
+
# rdoc-file=enumerator.c
|
496
|
+
# - to_proc()
|
497
|
+
# -->
|
498
|
+
# Returns a Proc object that takes arguments and yields them.
|
499
|
+
#
|
500
|
+
# This method is implemented so that a Yielder object can be directly passed to
|
501
|
+
# another method as a block argument.
|
502
|
+
#
|
503
|
+
# enum = Enumerator.new { |y|
|
504
|
+
# Dir.glob("*.rb") { |file|
|
505
|
+
# File.open(file) { |f| f.each_line(&y) }
|
506
|
+
# }
|
507
|
+
# }
|
508
|
+
#
|
260
509
|
def to_proc: () -> Proc
|
261
510
|
end
|
262
511
|
|
512
|
+
# <!-- rdoc-file=enumerator.c -->
|
513
|
+
# Enumerator::Chain is a subclass of Enumerator, which represents a chain of
|
514
|
+
# enumerables that works as a single enumerator.
|
515
|
+
#
|
516
|
+
# This type of objects can be created by Enumerable#chain and Enumerator#+.
|
517
|
+
#
|
263
518
|
class Enumerator::Chain[out Elem] < Object
|
264
519
|
include Enumerable[Elem]
|
265
520
|
|
521
|
+
# <!--
|
522
|
+
# rdoc-file=enumerator.c
|
523
|
+
# - obj.each(*args) { |...| ... } -> obj
|
524
|
+
# - obj.each(*args) -> enumerator
|
525
|
+
# -->
|
526
|
+
# Iterates over the elements of the first enumerable by calling the "each"
|
527
|
+
# method on it with the given arguments, then proceeds to the following
|
528
|
+
# enumerables in sequence until all of the enumerables are exhausted.
|
529
|
+
#
|
530
|
+
# If no block is given, returns an enumerator.
|
531
|
+
#
|
266
532
|
def each: () { (Elem) -> void } -> void
|
267
533
|
end
|