rbs 2.0.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (179) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +5 -0
  3. data/CHANGELOG.md +23 -0
  4. data/README.md +6 -1
  5. data/core/array.rbs +2866 -1086
  6. data/core/basic_object.rbs +150 -30
  7. data/core/binding.rbs +33 -0
  8. data/core/builtin.rbs +4 -4
  9. data/core/class.rbs +43 -5
  10. data/core/comparable.rbs +57 -0
  11. data/core/complex.rbs +170 -4
  12. data/core/constants.rbs +51 -0
  13. data/core/deprecated.rbs +7 -0
  14. data/core/dir.rbs +305 -20
  15. data/core/encoding.rbs +472 -77
  16. data/core/enumerable.rbs +2173 -234
  17. data/core/enumerator.rbs +448 -182
  18. data/core/env.rbs +448 -1
  19. data/core/errno.rbs +1 -10
  20. data/core/errors.rbs +152 -2
  21. data/core/exception.rbs +201 -127
  22. data/core/false_class.rbs +27 -0
  23. data/core/fiber.rbs +118 -37
  24. data/core/fiber_error.rbs +8 -9
  25. data/core/file.rbs +1059 -139
  26. data/core/file_test.rbs +287 -32
  27. data/core/float.rbs +776 -300
  28. data/core/gc.rbs +185 -34
  29. data/core/global_variables.rbs +5 -1
  30. data/core/hash.rbs +1582 -649
  31. data/core/integer.rbs +974 -204
  32. data/core/io/buffer.rbs +710 -0
  33. data/core/io/wait.rbs +29 -8
  34. data/core/io.rbs +2438 -417
  35. data/core/kernel.rbs +2315 -316
  36. data/core/marshal.rbs +37 -2
  37. data/core/match_data.rbs +123 -6
  38. data/core/math.rbs +126 -6
  39. data/core/method.rbs +226 -102
  40. data/core/module.rbs +421 -45
  41. data/core/nil_class.rbs +64 -0
  42. data/core/numeric.rbs +620 -142
  43. data/core/object.rbs +453 -81
  44. data/core/object_space.rbs +92 -2
  45. data/core/proc.rbs +482 -285
  46. data/core/process.rbs +443 -34
  47. data/core/ractor.rbs +232 -9
  48. data/core/random.rbs +151 -52
  49. data/core/range.rbs +885 -160
  50. data/core/rational.rbs +122 -6
  51. data/core/rb_config.rbs +14 -4
  52. data/core/refinement.rbs +44 -0
  53. data/core/regexp.rbs +156 -14
  54. data/core/ruby_vm.rbs +42 -3
  55. data/core/signal.rbs +78 -39
  56. data/core/string.rbs +2123 -567
  57. data/core/string_io.rbs +204 -0
  58. data/core/struct.rbs +283 -28
  59. data/core/symbol.rbs +304 -30
  60. data/core/thread.rbs +1288 -688
  61. data/core/thread_group.rbs +66 -10
  62. data/core/time.rbs +643 -217
  63. data/core/trace_point.rbs +100 -12
  64. data/core/true_class.rbs +24 -0
  65. data/core/unbound_method.rbs +73 -7
  66. data/core/warning.rbs +37 -12
  67. data/docs/CONTRIBUTING.md +40 -34
  68. data/docs/stdlib.md +3 -102
  69. data/lib/rbs/annotate/annotations.rb +197 -0
  70. data/lib/rbs/annotate/formatter.rb +80 -0
  71. data/lib/rbs/annotate/rdoc_annotator.rb +398 -0
  72. data/lib/rbs/annotate/rdoc_source.rb +120 -0
  73. data/lib/rbs/annotate.rb +6 -0
  74. data/lib/rbs/cli.rb +45 -1
  75. data/lib/rbs/definition_builder.rb +5 -1
  76. data/lib/rbs/location_aux.rb +12 -0
  77. data/lib/rbs/prototype/rb.rb +12 -0
  78. data/lib/rbs/version.rb +1 -1
  79. data/sig/annotate/annotations.rbs +102 -0
  80. data/sig/annotate/formatter.rbs +24 -0
  81. data/sig/annotate/rdoc_annotater.rbs +82 -0
  82. data/sig/annotate/rdoc_source.rbs +30 -0
  83. data/sig/cli.rbs +2 -0
  84. data/sig/collection/{collections.rbs → sources.rbs} +0 -0
  85. data/sig/location.rbs +6 -0
  86. data/sig/method_types.rbs +5 -1
  87. data/sig/polyfill.rbs +78 -0
  88. data/stdlib/abbrev/0/abbrev.rbs +6 -0
  89. data/stdlib/abbrev/0/array.rbs +26 -0
  90. data/stdlib/base64/0/base64.rbs +31 -0
  91. data/stdlib/benchmark/0/benchmark.rbs +74 -3
  92. data/stdlib/bigdecimal/0/big_decimal.rbs +614 -165
  93. data/stdlib/bigdecimal-math/0/big_math.rbs +41 -64
  94. data/stdlib/cgi/0/core.rbs +59 -0
  95. data/stdlib/coverage/0/coverage.rbs +164 -2
  96. data/stdlib/csv/0/csv.rbs +2862 -398
  97. data/stdlib/date/0/date.rbs +483 -25
  98. data/stdlib/date/0/date_time.rbs +187 -12
  99. data/stdlib/dbm/0/dbm.rbs +152 -17
  100. data/stdlib/digest/0/digest.rbs +146 -0
  101. data/stdlib/erb/0/erb.rbs +65 -245
  102. data/stdlib/fiber/0/fiber.rbs +73 -91
  103. data/stdlib/fileutils/0/fileutils.rbs +301 -1
  104. data/stdlib/find/0/find.rbs +9 -0
  105. data/stdlib/forwardable/0/forwardable.rbs +65 -1
  106. data/stdlib/io-console/0/io-console.rbs +227 -15
  107. data/stdlib/ipaddr/0/ipaddr.rbs +161 -0
  108. data/stdlib/json/0/json.rbs +1146 -144
  109. data/stdlib/logger/0/formatter.rbs +24 -0
  110. data/stdlib/logger/0/log_device.rbs +64 -0
  111. data/stdlib/logger/0/logger.rbs +165 -13
  112. data/stdlib/logger/0/period.rbs +10 -0
  113. data/stdlib/logger/0/severity.rbs +26 -0
  114. data/stdlib/monitor/0/monitor.rbs +163 -0
  115. data/stdlib/mutex_m/0/mutex_m.rbs +35 -6
  116. data/stdlib/net-http/0/net-http.rbs +1492 -683
  117. data/stdlib/nkf/0/nkf.rbs +372 -0
  118. data/stdlib/objspace/0/objspace.rbs +149 -90
  119. data/stdlib/openssl/0/openssl.rbs +8108 -71
  120. data/stdlib/optparse/0/optparse.rbs +487 -19
  121. data/stdlib/pathname/0/pathname.rbs +425 -124
  122. data/stdlib/prettyprint/0/prettyprint.rbs +120 -99
  123. data/stdlib/prime/0/integer-extension.rbs +20 -2
  124. data/stdlib/prime/0/prime.rbs +88 -21
  125. data/stdlib/pstore/0/pstore.rbs +102 -0
  126. data/stdlib/pty/0/pty.rbs +64 -14
  127. data/stdlib/resolv/0/resolv.rbs +420 -31
  128. data/stdlib/rubygems/0/basic_specification.rbs +4 -1
  129. data/stdlib/rubygems/0/config_file.rbs +33 -1
  130. data/stdlib/rubygems/0/dependency_installer.rbs +4 -3
  131. data/stdlib/rubygems/0/installer.rbs +13 -1
  132. data/stdlib/rubygems/0/path_support.rbs +4 -1
  133. data/stdlib/rubygems/0/platform.rbs +5 -1
  134. data/stdlib/rubygems/0/request_set.rbs +44 -2
  135. data/stdlib/rubygems/0/requirement.rbs +65 -2
  136. data/stdlib/rubygems/0/rubygems.rbs +407 -0
  137. data/stdlib/rubygems/0/source_list.rbs +13 -0
  138. data/stdlib/rubygems/0/specification.rbs +21 -1
  139. data/stdlib/rubygems/0/stream_ui.rbs +3 -1
  140. data/stdlib/rubygems/0/uninstaller.rbs +8 -1
  141. data/stdlib/rubygems/0/version.rbs +60 -157
  142. data/stdlib/securerandom/0/securerandom.rbs +44 -0
  143. data/stdlib/set/0/set.rbs +420 -106
  144. data/stdlib/shellwords/0/shellwords.rbs +55 -77
  145. data/stdlib/singleton/0/singleton.rbs +20 -0
  146. data/stdlib/socket/0/addrinfo.rbs +210 -9
  147. data/stdlib/socket/0/basic_socket.rbs +103 -11
  148. data/stdlib/socket/0/ip_socket.rbs +31 -9
  149. data/stdlib/socket/0/socket.rbs +586 -38
  150. data/stdlib/socket/0/tcp_server.rbs +22 -2
  151. data/stdlib/socket/0/tcp_socket.rbs +12 -1
  152. data/stdlib/socket/0/udp_socket.rbs +25 -2
  153. data/stdlib/socket/0/unix_server.rbs +22 -2
  154. data/stdlib/socket/0/unix_socket.rbs +45 -5
  155. data/stdlib/strscan/0/string_scanner.rbs +210 -9
  156. data/stdlib/tempfile/0/tempfile.rbs +58 -10
  157. data/stdlib/time/0/time.rbs +208 -116
  158. data/stdlib/timeout/0/timeout.rbs +10 -0
  159. data/stdlib/tmpdir/0/tmpdir.rbs +13 -4
  160. data/stdlib/tsort/0/cyclic.rbs +1 -0
  161. data/stdlib/tsort/0/interfaces.rbs +1 -0
  162. data/stdlib/tsort/0/tsort.rbs +42 -0
  163. data/stdlib/uri/0/common.rbs +57 -8
  164. data/stdlib/uri/0/file.rbs +55 -109
  165. data/stdlib/uri/0/ftp.rbs +6 -3
  166. data/stdlib/uri/0/generic.rbs +556 -327
  167. data/stdlib/uri/0/http.rbs +26 -115
  168. data/stdlib/uri/0/https.rbs +8 -102
  169. data/stdlib/uri/0/ldap.rbs +143 -137
  170. data/stdlib/uri/0/ldaps.rbs +8 -102
  171. data/stdlib/uri/0/mailto.rbs +3 -0
  172. data/stdlib/uri/0/rfc2396_parser.rbs +66 -26
  173. data/stdlib/uri/0/ws.rbs +6 -3
  174. data/stdlib/uri/0/wss.rbs +5 -3
  175. data/stdlib/yaml/0/dbm.rbs +151 -87
  176. data/stdlib/yaml/0/store.rbs +6 -0
  177. data/stdlib/zlib/0/zlib.rbs +90 -31
  178. metadata +17 -5
  179. 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 [Enumerator](Enumerator) can be created by the
4
- # following methods.
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
- # - Kernel\#enum\_for
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
- # - [::new](Enumerator#method-c-new)
14
+ # enumerator = %w(one two three).each
15
+ # puts enumerator.class # => Enumerator
11
16
  #
12
- # Most methods have two forms: a block form where the contents are
13
- # evaluated for each item in the enumeration, and a non-block form which
14
- # returns a new [Enumerator](Enumerator) wrapping the
15
- # iteration.
17
+ # enumerator.each_with_object("foo") do |item, obj|
18
+ # puts "#{obj}: #{item}"
19
+ # end
16
20
  #
17
- # ```ruby
18
- # enumerator = %w(one two three).each
19
- # puts enumerator.class # => Enumerator
21
+ # # foo: one
22
+ # # foo: two
23
+ # # foo: three
20
24
  #
21
- # enumerator.each_with_object("foo") do |item, obj|
22
- # puts "#{obj}: #{item}"
23
- # end
25
+ # enum_with_obj = enumerator.each_with_object("foo")
26
+ # puts enum_with_obj.class # => Enumerator
24
27
  #
25
- # # foo: one
26
- # # foo: two
27
- # # foo: three
28
+ # enum_with_obj.each do |item, obj|
29
+ # puts "#{obj}: #{item}"
30
+ # end
28
31
  #
29
- # enum_with_obj = enumerator.each_with_object("foo")
30
- # puts enum_with_obj.class # => Enumerator
32
+ # # foo: one
33
+ # # foo: two
34
+ # # foo: three
31
35
  #
32
- # enum_with_obj.each do |item, obj|
33
- # puts "#{obj}: #{item}"
34
- # end
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
- # # foo: one
37
- # # foo: two
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
- # This allows you to chain Enumerators together. For example, you can map
42
- # a list's elements to strings containing the index and the element as a
43
- # string via:
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
- # ```ruby
46
- # puts %w[foo bar baz].map.with_index { |w, i| "#{i}:#{w}" }
47
- # # => ["0:foo", "1:bar", "2:baz"]
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
- # An [Enumerator](Enumerator) can also be used as an
51
- # external iterator. For example,
52
- # [\#next](Enumerator#method-i-next) returns the next
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
- # ```ruby
58
- # e = [1,2,3].each # returns an enumerator object.
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
- # ```ruby
68
- # def ext_each(e)
69
- # while true
70
- # begin
71
- # vs = e.next_values
72
- # rescue StopIteration
73
- # return $!.result
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
- # y = yield(*vs)
76
- # e.feed y
77
- # end
78
- # end
79
- #
80
- # o = Object.new
81
- #
82
- # def o.each
83
- # puts yield
84
- # puts yield(1)
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
- # Creates a printable version of *e* .
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
- # Returns the next object in the enumerator, and move the internal
112
- # position forward. When the position reached at the end,
113
- # [StopIteration](https://ruby-doc.org/core-2.6.3/StopIteration.html) is
114
- # raised.
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
- # Returns the next object as an array in the enumerator, and move the
134
- # internal position forward. When the position reached at the end,
135
- # [StopIteration](https://ruby-doc.org/core-2.6.3/StopIteration.html) is
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
- # This method can be used to distinguish `yield` and `yield nil` .
139
- #
140
- #
141
- # ```ruby
142
- # o = Object.new
143
- # def o.each
144
- # yield
145
- # yield 1
146
- # yield 1, 2
147
- # yield nil
148
- # yield [1, 2]
149
- # end
150
- # e = o.to_enum
151
- # p e.next_values
152
- # p e.next_values
153
- # p e.next_values
154
- # p e.next_values
155
- # p e.next_values
156
- # e = o.to_enum
157
- # p e.next
158
- # p e.next
159
- # p e.next
160
- # p e.next
161
- # p e.next
162
- #
163
- # ## yield args next_values next
164
- # # yield [] nil
165
- # # yield 1 [1] 1
166
- # # yield 1, 2 [1, 2] [1, 2]
167
- # # yield nil [nil] nil
168
- # # yield [1, 2] [[1, 2]] [1, 2]
169
- # ```
170
- #
171
- # Note that `next_values` does not affect other non-external enumeration
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
- # Returns the next object in the enumerator, but doesn’t move the internal
178
- # position forward. If the position is already at the end,
179
- # [StopIteration](https://ruby-doc.org/core-2.6.3/StopIteration.html) is
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
- # Returns the next object as an array, similar to
197
- # [\#next\_values](Enumerator.downloaded.ruby_doc#method-i-next_values),
198
- # but doesn’t move the internal position forward. If the position is
199
- # already at the end,
200
- # [StopIteration](https://ruby-doc.org/core-2.6.3/StopIteration.html) is
201
- # raised.
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 rewind method, it is called.
338
+ # If the enclosed object responds to a "rewind" method, it is called.
339
+ #
226
340
  def rewind: () -> self
227
341
 
228
- # Returns the size of the enumerator, or `nil` if it can’t be calculated
229
- # lazily.
230
- #
231
- # ```ruby
232
- # (1..100).to_a.permutation(4).size # => 94109400
233
- # loop.size # => Float::INFINITY
234
- # (1..100).drop_while.size # => nil
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