rbs 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (132) hide show
  1. checksums.yaml +7 -0
  2. data/.github/workflows/ruby.yml +28 -0
  3. data/.gitignore +12 -0
  4. data/.rubocop.yml +15 -0
  5. data/BSDL +22 -0
  6. data/CHANGELOG.md +9 -0
  7. data/COPYING +56 -0
  8. data/Gemfile +6 -0
  9. data/README.md +93 -0
  10. data/Rakefile +142 -0
  11. data/bin/annotate-with-rdoc +157 -0
  12. data/bin/console +14 -0
  13. data/bin/query-rdoc +103 -0
  14. data/bin/setup +10 -0
  15. data/bin/sort +89 -0
  16. data/bin/test_runner.rb +16 -0
  17. data/docs/CONTRIBUTING.md +97 -0
  18. data/docs/sigs.md +148 -0
  19. data/docs/stdlib.md +152 -0
  20. data/docs/syntax.md +528 -0
  21. data/exe/rbs +7 -0
  22. data/lib/rbs.rb +64 -0
  23. data/lib/rbs/ast/annotation.rb +27 -0
  24. data/lib/rbs/ast/comment.rb +27 -0
  25. data/lib/rbs/ast/declarations.rb +395 -0
  26. data/lib/rbs/ast/members.rb +362 -0
  27. data/lib/rbs/buffer.rb +50 -0
  28. data/lib/rbs/builtin_names.rb +55 -0
  29. data/lib/rbs/cli.rb +558 -0
  30. data/lib/rbs/constant.rb +26 -0
  31. data/lib/rbs/constant_table.rb +150 -0
  32. data/lib/rbs/definition.rb +170 -0
  33. data/lib/rbs/definition_builder.rb +919 -0
  34. data/lib/rbs/environment.rb +281 -0
  35. data/lib/rbs/environment_loader.rb +136 -0
  36. data/lib/rbs/environment_walker.rb +124 -0
  37. data/lib/rbs/errors.rb +187 -0
  38. data/lib/rbs/location.rb +102 -0
  39. data/lib/rbs/method_type.rb +123 -0
  40. data/lib/rbs/namespace.rb +91 -0
  41. data/lib/rbs/parser.y +1344 -0
  42. data/lib/rbs/prototype/rb.rb +553 -0
  43. data/lib/rbs/prototype/rbi.rb +587 -0
  44. data/lib/rbs/prototype/runtime.rb +381 -0
  45. data/lib/rbs/substitution.rb +46 -0
  46. data/lib/rbs/test.rb +26 -0
  47. data/lib/rbs/test/errors.rb +61 -0
  48. data/lib/rbs/test/hook.rb +294 -0
  49. data/lib/rbs/test/setup.rb +58 -0
  50. data/lib/rbs/test/spy.rb +325 -0
  51. data/lib/rbs/test/test_helper.rb +183 -0
  52. data/lib/rbs/test/type_check.rb +254 -0
  53. data/lib/rbs/type_name.rb +70 -0
  54. data/lib/rbs/types.rb +936 -0
  55. data/lib/rbs/variance_calculator.rb +138 -0
  56. data/lib/rbs/vendorer.rb +47 -0
  57. data/lib/rbs/version.rb +3 -0
  58. data/lib/rbs/writer.rb +269 -0
  59. data/lib/ruby/signature.rb +7 -0
  60. data/rbs.gemspec +46 -0
  61. data/stdlib/abbrev/abbrev.rbs +60 -0
  62. data/stdlib/base64/base64.rbs +71 -0
  63. data/stdlib/benchmark/benchmark.rbs +372 -0
  64. data/stdlib/builtin/array.rbs +1997 -0
  65. data/stdlib/builtin/basic_object.rbs +280 -0
  66. data/stdlib/builtin/binding.rbs +177 -0
  67. data/stdlib/builtin/builtin.rbs +45 -0
  68. data/stdlib/builtin/class.rbs +145 -0
  69. data/stdlib/builtin/comparable.rbs +116 -0
  70. data/stdlib/builtin/complex.rbs +400 -0
  71. data/stdlib/builtin/constants.rbs +37 -0
  72. data/stdlib/builtin/data.rbs +5 -0
  73. data/stdlib/builtin/deprecated.rbs +2 -0
  74. data/stdlib/builtin/dir.rbs +413 -0
  75. data/stdlib/builtin/encoding.rbs +607 -0
  76. data/stdlib/builtin/enumerable.rbs +404 -0
  77. data/stdlib/builtin/enumerator.rbs +260 -0
  78. data/stdlib/builtin/errno.rbs +781 -0
  79. data/stdlib/builtin/errors.rbs +582 -0
  80. data/stdlib/builtin/exception.rbs +194 -0
  81. data/stdlib/builtin/false_class.rbs +40 -0
  82. data/stdlib/builtin/fiber.rbs +68 -0
  83. data/stdlib/builtin/fiber_error.rbs +12 -0
  84. data/stdlib/builtin/file.rbs +1076 -0
  85. data/stdlib/builtin/file_test.rbs +59 -0
  86. data/stdlib/builtin/float.rbs +696 -0
  87. data/stdlib/builtin/gc.rbs +243 -0
  88. data/stdlib/builtin/hash.rbs +1029 -0
  89. data/stdlib/builtin/integer.rbs +707 -0
  90. data/stdlib/builtin/io.rbs +683 -0
  91. data/stdlib/builtin/kernel.rbs +576 -0
  92. data/stdlib/builtin/marshal.rbs +161 -0
  93. data/stdlib/builtin/match_data.rbs +271 -0
  94. data/stdlib/builtin/math.rbs +369 -0
  95. data/stdlib/builtin/method.rbs +185 -0
  96. data/stdlib/builtin/module.rbs +1104 -0
  97. data/stdlib/builtin/nil_class.rbs +82 -0
  98. data/stdlib/builtin/numeric.rbs +409 -0
  99. data/stdlib/builtin/object.rbs +824 -0
  100. data/stdlib/builtin/proc.rbs +429 -0
  101. data/stdlib/builtin/process.rbs +1227 -0
  102. data/stdlib/builtin/random.rbs +267 -0
  103. data/stdlib/builtin/range.rbs +226 -0
  104. data/stdlib/builtin/rational.rbs +424 -0
  105. data/stdlib/builtin/rb_config.rbs +57 -0
  106. data/stdlib/builtin/regexp.rbs +1083 -0
  107. data/stdlib/builtin/ruby_vm.rbs +14 -0
  108. data/stdlib/builtin/signal.rbs +55 -0
  109. data/stdlib/builtin/string.rbs +1901 -0
  110. data/stdlib/builtin/string_io.rbs +284 -0
  111. data/stdlib/builtin/struct.rbs +40 -0
  112. data/stdlib/builtin/symbol.rbs +228 -0
  113. data/stdlib/builtin/thread.rbs +1108 -0
  114. data/stdlib/builtin/thread_group.rbs +23 -0
  115. data/stdlib/builtin/time.rbs +1047 -0
  116. data/stdlib/builtin/trace_point.rbs +290 -0
  117. data/stdlib/builtin/true_class.rbs +46 -0
  118. data/stdlib/builtin/unbound_method.rbs +153 -0
  119. data/stdlib/builtin/warning.rbs +17 -0
  120. data/stdlib/coverage/coverage.rbs +62 -0
  121. data/stdlib/csv/csv.rbs +773 -0
  122. data/stdlib/erb/erb.rbs +392 -0
  123. data/stdlib/find/find.rbs +40 -0
  124. data/stdlib/ipaddr/ipaddr.rbs +247 -0
  125. data/stdlib/json/json.rbs +335 -0
  126. data/stdlib/pathname/pathname.rbs +1093 -0
  127. data/stdlib/prime/integer-extension.rbs +23 -0
  128. data/stdlib/prime/prime.rbs +188 -0
  129. data/stdlib/securerandom/securerandom.rbs +9 -0
  130. data/stdlib/set/set.rbs +301 -0
  131. data/stdlib/tmpdir/tmpdir.rbs +53 -0
  132. metadata +292 -0
@@ -0,0 +1,404 @@
1
+ # The `Enumerable` mixin provides collection classes with several
2
+ # traversal and searching methods, and with the ability to sort. The class
3
+ # must provide a method `each`, which yields successive members of the
4
+ # collection. If `Enumerable#max`, `#min`, or `#sort` is used, the
5
+ # objects in the collection must also implement a meaningful `<=>`
6
+ # operator, as these methods rely on an ordering between members of the
7
+ # collection.
8
+ module Enumerable[unchecked out Elem, out Return]: _Each[Elem, Return]
9
+ # Passes each element of the collection to the given block. The method
10
+ # returns `true` if the block never returns `false` or `nil` . If the
11
+ # block is not given, Ruby adds an implicit block of `{ |obj| obj }` which
12
+ # will cause [all?](Enumerable.downloaded.ruby_doc#method-i-all-3F) to
13
+ # return `true` when none of the collection members are `false` or `nil` .
14
+ #
15
+ # If instead a pattern is supplied, the method returns whether `pattern
16
+ # === element` for every collection member.
17
+ #
18
+ # %w[ant bear cat].all? { |word| word.length >= 3 } #=> true
19
+ # %w[ant bear cat].all? { |word| word.length >= 4 } #=> false
20
+ # %w[ant bear cat].all?(/t/) #=> false
21
+ # [1, 2i, 3.14].all?(Numeric) #=> true
22
+ # [nil, true, 99].all? #=> false
23
+ # [].all? #=> true
24
+ def all?: () -> bool
25
+ | () { (Elem arg0) -> untyped } -> bool
26
+
27
+ # Passes each element of the collection to the given block. The method
28
+ # returns `true` if the block ever returns a value other than `false` or
29
+ # `nil` . If the block is not given, Ruby adds an implicit block of `{
30
+ # |obj| obj }` that will cause
31
+ # [any?](Enumerable.downloaded.ruby_doc#method-i-any-3F) to return `true`
32
+ # if at least one of the collection members is not `false` or `nil` .
33
+ #
34
+ # If instead a pattern is supplied, the method returns whether `pattern
35
+ # === element` for any collection member.
36
+ #
37
+ # ```ruby
38
+ # %w[ant bear cat].any? { |word| word.length >= 3 } #=> true
39
+ # %w[ant bear cat].any? { |word| word.length >= 4 } #=> true
40
+ # %w[ant bear cat].any?(/d/) #=> false
41
+ # [nil, true, 99].any?(Integer) #=> true
42
+ # [nil, true, 99].any? #=> true
43
+ # [].any? #=> false
44
+ # ```
45
+ def `any?`: () -> bool
46
+ | () { (Elem arg0) -> untyped } -> bool
47
+
48
+ def collect: [U] () { (Elem arg0) -> U } -> ::Array[U]
49
+ | () -> ::Enumerator[Elem, Return]
50
+
51
+ def collect_concat: [U] () { (Elem arg0) -> ::Enumerator[U, untyped] } -> ::Array[U]
52
+
53
+ # Returns the number of items in `enum` through enumeration. If an
54
+ # argument is given, the number of items in `enum` that are equal to
55
+ # `item` are counted. If a block is given, it counts the number of
56
+ # elements yielding a true value.
57
+ #
58
+ # ```ruby
59
+ # ary = [1, 2, 4, 2]
60
+ # ary.count #=> 4
61
+ # ary.count(2) #=> 2
62
+ # ary.count{ |x| x%2==0 } #=> 3
63
+ # ```
64
+ def count: () -> Integer
65
+ | (?untyped arg0) -> Integer
66
+ | () { (Elem arg0) -> untyped } -> Integer
67
+
68
+ def cycle: (?Integer n) { (Elem arg0) -> untyped } -> NilClass
69
+ | (?Integer n) -> ::Enumerator[Elem, Return]
70
+
71
+ def detect: (?Proc ifnone) { (Elem arg0) -> untyped } -> Elem?
72
+ | (?Proc ifnone) -> ::Enumerator[Elem, Return]
73
+
74
+ def drop: (Integer n) -> ::Array[Elem]
75
+
76
+ def drop_while: () { (Elem arg0) -> untyped } -> ::Array[Elem]
77
+ | () -> ::Enumerator[Elem, Return]
78
+
79
+ def each_cons: (Integer n) { (::Array[Elem] arg0) -> untyped } -> NilClass
80
+ | (Integer n) -> ::Enumerator[::Array[Elem], Return]
81
+
82
+ def each_with_index: () { (Elem arg0, Integer arg1) -> untyped } -> ::Enumerable[Elem, Return]
83
+ | () -> ::Enumerator[[ Elem, Integer ], Return]
84
+
85
+ def each_with_object: [U] (U arg0) { (Elem arg0, untyped arg1) -> untyped } -> U
86
+ | [U] (U arg0) -> ::Enumerator[[ Elem, U ], Return]
87
+
88
+ # Returns an array containing the items in *enum* .
89
+ #
90
+ # ```ruby
91
+ # (1..7).to_a #=> [1, 2, 3, 4, 5, 6, 7]
92
+ # { 'a'=>1, 'b'=>2, 'c'=>3 }.to_a #=> [["a", 1], ["b", 2], ["c", 3]]
93
+ #
94
+ # require 'prime'
95
+ # Prime.entries 10 #=> [2, 3, 5, 7]
96
+ # ```
97
+ def entries: () -> ::Array[Elem]
98
+
99
+ def find_all: () { (Elem arg0) -> untyped } -> ::Array[Elem]
100
+ | () -> ::Enumerator[Elem, Return]
101
+
102
+ alias select find_all
103
+ alias filter find_all
104
+
105
+ def find_index: (?untyped value) -> Integer?
106
+ | () { (Elem arg0) -> untyped } -> Integer?
107
+ | () -> ::Enumerator[Elem, Return]
108
+
109
+ # Returns the first element, or the first `n` elements, of the enumerable.
110
+ # If the enumerable is empty, the first form returns `nil`, and the
111
+ # second form returns an empty array.
112
+ #
113
+ # ```ruby
114
+ # %w[foo bar baz].first #=> "foo"
115
+ # %w[foo bar baz].first(2) #=> ["foo", "bar"]
116
+ # %w[foo bar baz].first(10) #=> ["foo", "bar", "baz"]
117
+ # [].first #=> nil
118
+ # [].first(10) #=> []
119
+ # ```
120
+ def first: () -> Elem?
121
+ | (?Integer n) -> ::Array[Elem]?
122
+
123
+ def grep: (untyped arg0) -> ::Array[Elem]
124
+ | [U] (untyped arg0) { (Elem arg0) -> U } -> ::Array[U]
125
+
126
+ def grep_v: (untyped arg0) -> ::Array[Integer]
127
+ | [U] (untyped arg0) { (Elem arg0) -> U } -> ::Array[U]
128
+
129
+ def group_by: [U] () { (Elem arg0) -> U } -> ::Hash[U, ::Array[Elem]]
130
+ | () -> ::Enumerator[Elem, Return]
131
+
132
+ def `include?`: (untyped arg0) -> bool
133
+
134
+ def inject: (untyped init, Symbol method) -> untyped
135
+ | (Symbol method) -> untyped
136
+ | [A] (A initial) { (A, Elem) -> A } -> A
137
+ | () { (Elem, Elem) -> Elem } -> Elem
138
+
139
+ # Returns the object in *enum* with the maximum value. The first form
140
+ # assumes all objects implement `Comparable` ; the second uses the block
141
+ # to return *a \<=\> b* .
142
+ #
143
+ # ```ruby
144
+ # a = %w(albatross dog horse)
145
+ # a.max #=> "horse"
146
+ # a.max { |a, b| a.length <=> b.length } #=> "albatross"
147
+ # ```
148
+ #
149
+ # If the `n` argument is given, maximum `n` elements are returned as an
150
+ # array, sorted in descending order.
151
+ #
152
+ # ```ruby
153
+ # a = %w[albatross dog horse]
154
+ # a.max(2) #=> ["horse", "dog"]
155
+ # a.max(2) {|a, b| a.length <=> b.length } #=> ["albatross", "horse"]
156
+ # [5, 1, 3, 4, 2].max(3) #=> [5, 4, 3]
157
+ # ```
158
+ def max: () -> Elem?
159
+ | () { (Elem arg0, Elem arg1) -> Integer } -> Elem?
160
+ | (?Integer arg0) -> ::Array[Elem]
161
+ | (?Integer arg0) { (Elem arg0, Elem arg1) -> Integer } -> ::Array[Elem]
162
+
163
+ def max_by: () -> ::Enumerator[Elem, Return]
164
+ | () { (Elem arg0) -> (Comparable | ::Array[untyped]) } -> Elem?
165
+ | (?Integer arg0) -> ::Enumerator[Elem, Return]
166
+ | (?Integer arg0) { (Elem arg0) -> (Comparable | ::Array[untyped]) } -> ::Array[Elem]
167
+
168
+ # Returns the object in *enum* with the minimum value. The first form
169
+ # assumes all objects implement `Comparable` ; the second uses the block
170
+ # to return *a \<=\> b* .
171
+ #
172
+ # ```ruby
173
+ # a = %w(albatross dog horse)
174
+ # a.min #=> "albatross"
175
+ # a.min { |a, b| a.length <=> b.length } #=> "dog"
176
+ # ```
177
+ #
178
+ # If the `n` argument is given, minimum `n` elements are returned as a
179
+ # sorted array.
180
+ #
181
+ # ```ruby
182
+ # a = %w[albatross dog horse]
183
+ # a.min(2) #=> ["albatross", "dog"]
184
+ # a.min(2) {|a, b| a.length <=> b.length } #=> ["dog", "horse"]
185
+ # [5, 1, 3, 4, 2].min(3) #=> [1, 2, 3]
186
+ # ```
187
+ def min: () -> Elem?
188
+ | () { (Elem arg0, Elem arg1) -> Integer } -> Elem?
189
+ | (?Integer arg0) -> ::Array[Elem]
190
+ | (?Integer arg0) { (Elem arg0, Elem arg1) -> Integer } -> ::Array[Elem]
191
+
192
+ def min_by: () -> ::Enumerator[Elem, Return]
193
+ | () { (Elem arg0) -> (Comparable | ::Array[untyped]) } -> Elem?
194
+ | (?Integer arg0) -> ::Enumerator[Elem, Return]
195
+ | (?Integer arg0) { (Elem arg0) -> (Comparable | ::Array[untyped]) } -> ::Array[Elem]
196
+
197
+ # Returns a two element array which contains the minimum and the maximum
198
+ # value in the enumerable. The first form assumes all objects implement
199
+ # `Comparable` ; the second uses the block to return *a \<=\> b* .
200
+ #
201
+ # ```ruby
202
+ # a = %w(albatross dog horse)
203
+ # a.minmax #=> ["albatross", "horse"]
204
+ # a.minmax { |a, b| a.length <=> b.length } #=> ["dog", "albatross"]
205
+ # ```
206
+ def minmax: () -> [ Elem?, Elem? ]
207
+ | () { (Elem arg0, Elem arg1) -> Integer } -> [ Elem?, Elem? ]
208
+
209
+ def minmax_by: () -> [ Elem?, Elem? ]
210
+ | () { (Elem arg0) -> (Comparable | ::Array[untyped]) } -> ::Enumerator[Elem, Return]
211
+
212
+ # Passes each element of the collection to the given block. The method
213
+ # returns `true` if the block never returns `true` for all elements. If
214
+ # the block is not given, `none?` will return `true` only if none of the
215
+ # collection members is true.
216
+ #
217
+ # If instead a pattern is supplied, the method returns whether `pattern
218
+ # === element` for none of the collection members.
219
+ #
220
+ # ```ruby
221
+ # %w{ant bear cat}.none? { |word| word.length == 5 } #=> true
222
+ # %w{ant bear cat}.none? { |word| word.length >= 4 } #=> false
223
+ # %w{ant bear cat}.none?(/d/) #=> true
224
+ # [1, 3.14, 42].none?(Float) #=> false
225
+ # [].none? #=> true
226
+ # [nil].none? #=> true
227
+ # [nil, false].none? #=> true
228
+ # [nil, false, true].none? #=> false
229
+ # ```
230
+ def none?: () -> bool
231
+ | () { (Elem arg0) -> untyped } -> bool
232
+
233
+ # Passes each element of the collection to the given block. The method
234
+ # returns `true` if the block returns `true` exactly once. If the block is
235
+ # not given, `one?` will return `true` only if exactly one of the
236
+ # collection members is true.
237
+ #
238
+ # If instead a pattern is supplied, the method returns whether `pattern
239
+ # === element` for exactly one collection member.
240
+ #
241
+ # ```ruby
242
+ # %w{ant bear cat}.one? { |word| word.length == 4 } #=> true
243
+ # %w{ant bear cat}.one? { |word| word.length > 4 } #=> false
244
+ # %w{ant bear cat}.one? { |word| word.length < 4 } #=> false
245
+ # %w{ant bear cat}.one?(/t/) #=> false
246
+ # [ nil, true, 99 ].one? #=> false
247
+ # [ nil, true, false ].one? #=> true
248
+ # [ nil, true, 99 ].one?(Integer) #=> true
249
+ # [].one? #=> false
250
+ # ```
251
+ def one?: () -> bool
252
+ | () { (Elem arg0) -> untyped } -> bool
253
+
254
+ def partition: () { (Elem arg0) -> untyped } -> [ ::Array[Elem], ::Array[Elem] ]
255
+ | () -> ::Enumerator[Elem, Return]
256
+
257
+ def reject: () { (Elem arg0) -> untyped } -> ::Array[Elem]
258
+ | () -> ::Enumerator[Elem, Return]
259
+
260
+ def reverse_each: () { (Elem arg0) -> untyped } -> ::Enumerator[Elem, Return]
261
+ | () -> ::Enumerator[Elem, Return]
262
+
263
+ # Returns an array containing the items in *enum* sorted.
264
+ #
265
+ # Comparisons for the sort will be done using the items’ own `<=>`
266
+ # operator or using an optional code block.
267
+ #
268
+ # The block must implement a comparison between `a` and `b` and return an
269
+ # integer less than 0 when `b` follows `a`, `0` when `a` and `b` are
270
+ # equivalent, or an integer greater than 0 when `a` follows `b` .
271
+ #
272
+ # The result is not guaranteed to be stable. When the comparison of two
273
+ # elements returns `0`, the order of the elements is unpredictable.
274
+ #
275
+ # ```ruby
276
+ # %w(rhea kea flea).sort #=> ["flea", "kea", "rhea"]
277
+ # (1..10).sort { |a, b| b <=> a } #=> [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
278
+ # ```
279
+ #
280
+ # See also [\#sort\_by](Enumerable.downloaded.ruby_doc#method-i-sort_by).
281
+ # It implements a Schwartzian transform which is useful when key
282
+ # computation or comparison is expensive.
283
+ def sort: () -> ::Array[Elem]
284
+ | () { (Elem arg0, Elem arg1) -> Integer } -> ::Array[Elem]
285
+
286
+ def sort_by: () { (Elem arg0) -> (Comparable | ::Array[untyped]) } -> ::Array[Elem]
287
+ | () -> ::Enumerator[Elem, Return]
288
+
289
+ def take: (Integer n) -> ::Array[Elem]?
290
+
291
+ def take_while: () { (Elem arg0) -> untyped } -> ::Array[Elem]
292
+ | () -> ::Enumerator[Elem, Return]
293
+
294
+ # Implemented in C++
295
+ # Returns the result of interpreting *enum* as a list of `[key, value]`
296
+ # pairs.
297
+ #
298
+ # %i[hello world].each_with_index.to_h
299
+ # # => {:hello => 0, :world => 1}
300
+ #
301
+ # If a block is given, the results of the block on each element of the
302
+ # enum will be used as pairs.
303
+ #
304
+ # ```ruby
305
+ # (1..5).to_h {|x| [x, x ** 2]}
306
+ # #=> {1=>1, 2=>4, 3=>9, 4=>16, 5=>25}
307
+ # ```
308
+ def to_h: () -> ::Hash[untyped, untyped]
309
+
310
+ def each_slice: (Integer n) { (::Array[Elem] arg0) -> untyped } -> NilClass
311
+ | (Integer n) -> ::Enumerator[::Array[Elem], Return]
312
+
313
+ def find: (?Proc ifnone) { (Elem arg0) -> untyped } -> Elem?
314
+ | (?Proc ifnone) -> ::Enumerator[Elem, Return]
315
+
316
+ def flat_map: [U] () { (Elem arg0) -> U } -> U
317
+ | () -> ::Enumerator[Elem, Return]
318
+
319
+ def map: [U] () { (Elem arg0) -> U } -> ::Array[U]
320
+ | () -> ::Enumerator[Elem, Return]
321
+
322
+ def member?: (untyped arg0) -> bool
323
+
324
+ alias reduce inject
325
+
326
+ # Returns an array containing the items in *enum* .
327
+ #
328
+ # ```ruby
329
+ # (1..7).to_a #=> [1, 2, 3, 4, 5, 6, 7]
330
+ # { 'a'=>1, 'b'=>2, 'c'=>3 }.to_a #=> [["a", 1], ["b", 2], ["c", 3]]
331
+ #
332
+ # require 'prime'
333
+ # Prime.entries 10 #=> [2, 3, 5, 7]
334
+ # ```
335
+ def to_a: () -> ::Array[Elem]
336
+
337
+ # Returns a lazy enumerator, whose methods map/collect,
338
+ # flat\_map/collect\_concat, select/find\_all, reject, grep,
339
+ # [\#grep\_v](Enumerable.downloaded.ruby_doc#method-i-grep_v), zip, take,
340
+ # [\#take\_while](Enumerable.downloaded.ruby_doc#method-i-take_while),
341
+ # drop, and
342
+ # [\#drop\_while](Enumerable.downloaded.ruby_doc#method-i-drop_while)
343
+ # enumerate values only on an as-needed basis. However, if a block is
344
+ # given to zip, values are enumerated immediately.
345
+ #
346
+ #
347
+ # The following program finds pythagorean triples:
348
+ #
349
+ # ```ruby
350
+ # def pythagorean_triples
351
+ # (1..Float::INFINITY).lazy.flat_map {|z|
352
+ # (1..z).flat_map {|x|
353
+ # (x..z).select {|y|
354
+ # x**2 + y**2 == z**2
355
+ # }.map {|y|
356
+ # [x, y, z]
357
+ # }
358
+ # }
359
+ # }
360
+ # end
361
+ # # show first ten pythagorean triples
362
+ # p pythagorean_triples.take(10).force # take is lazy, so force is needed
363
+ # p pythagorean_triples.first(10) # first is eager
364
+ # # show pythagorean triples less than 100
365
+ # p pythagorean_triples.take_while { |*, z| z < 100 }.force
366
+ # ```
367
+ def lazy: () -> Enumerator::Lazy[Elem, Return]
368
+
369
+ def uniq: () -> ::Array[Elem]
370
+ | () { (Elem item) -> untyped } -> ::Array[Elem]
371
+
372
+ def sum: () -> (Elem | Integer)
373
+ | [T] () { (Elem arg0) -> T } -> (Integer | T)
374
+ | [T] (?T arg0) -> (Elem | T)
375
+ | [U] (?U arg0) { (Elem arg0) -> U } -> U
376
+
377
+ def filter_map: [U] () { (Elem arg0) -> U? } -> ::Array[U]
378
+ | () -> ::Enumerator[Elem, Return]
379
+
380
+ def chain: (*self enumerables) -> ::Enumerator::Chain[Elem, ::Array[self]]
381
+
382
+ def tally: () -> ::Hash[Elem, Integer]
383
+
384
+ def each_entry: () -> ::Enumerator[Elem, Return]
385
+ | () { (Elem arg0) -> untyped } -> self
386
+
387
+ # variadic type parameter is not supported yet
388
+ # https://github.com/ruby/ruby-signature/issues/21
389
+ def zip: [Elem2, Return2] (::Enumerable[Elem2, Return2] enum) -> ::Array[[Elem, Elem2 | nil]]
390
+ | [U, Elem2, Return2] (::Enumerable[Elem2, Return2]) { ([Elem, Elem2 | nil]) -> U } -> nil
391
+
392
+ def chunk: () -> ::Enumerator[Elem, Return]
393
+ | [U] () { (Elem elt) -> U } -> ::Enumerator[[U, Array[Elem]], void]
394
+
395
+ def chunk_while: () { (Elem elt_before, Elem elt_after) -> bool } -> ::Enumerator[::Array[Elem], void]
396
+
397
+ def slice_when: () { (Elem elt_before, Elem elt_after) -> bool } -> ::Enumerator[::Array[Elem], void]
398
+
399
+ def slice_after: (untyped pattern) -> ::Enumerator[::Array[Elem], void]
400
+ | () { (Elem elt) -> bool } -> ::Enumerator[::Array[Elem], void]
401
+
402
+ def slice_before: (untyped pattern) -> ::Enumerator[::Array[Elem], void]
403
+ | () { (Elem elt) -> bool } -> ::Enumerator[::Array[Elem], void]
404
+ end
@@ -0,0 +1,260 @@
1
+ # A class which allows both internal and external iteration.
2
+ #
3
+ # An [Enumerator](Enumerator) can be created by the
4
+ # following methods.
5
+ #
6
+ # - Kernel\#to\_enum
7
+ #
8
+ # - Kernel\#enum\_for
9
+ #
10
+ # - [::new](Enumerator#method-c-new)
11
+ #
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.
16
+ #
17
+ # ```ruby
18
+ # enumerator = %w(one two three).each
19
+ # puts enumerator.class # => Enumerator
20
+ #
21
+ # enumerator.each_with_object("foo") do |item, obj|
22
+ # puts "#{obj}: #{item}"
23
+ # end
24
+ #
25
+ # # foo: one
26
+ # # foo: two
27
+ # # foo: three
28
+ #
29
+ # enum_with_obj = enumerator.each_with_object("foo")
30
+ # puts enum_with_obj.class # => Enumerator
31
+ #
32
+ # enum_with_obj.each do |item, obj|
33
+ # puts "#{obj}: #{item}"
34
+ # end
35
+ #
36
+ # # foo: one
37
+ # # foo: two
38
+ # # foo: three
39
+ # ```
40
+ #
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:
44
+ #
45
+ # ```ruby
46
+ # puts %w[foo bar baz].map.with_index { |w, i| "#{i}:#{w}" }
47
+ # # => ["0:foo", "1:bar", "2:baz"]
48
+ # ```
49
+ #
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.
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
+ # ```
64
+ #
65
+ # You can use this to implement an internal iterator as follows:
66
+ #
67
+ # ```ruby
68
+ # def ext_each(e)
69
+ # while true
70
+ # begin
71
+ # vs = e.next_values
72
+ # rescue StopIteration
73
+ # return $!.result
74
+ # 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
+ # ```
98
+ class Enumerator[unchecked out Elem, out Return] < Object
99
+ include Enumerable[Elem, Return]
100
+
101
+ def each: () { (Elem arg0) -> untyped } -> Return
102
+ | () -> self
103
+
104
+ def feed: (Elem arg0) -> NilClass
105
+
106
+ def initialize: (?Integer arg0) { (Enumerator::Yielder arg0) -> void } -> void
107
+
108
+ # Creates a printable version of *e* .
109
+ def inspect: () -> String
110
+
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.
115
+ #
116
+ #
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
+ def next: () -> Elem
132
+
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
136
+ # raised.
137
+ #
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
+ # .
175
+ def next_values: () -> ::Array[Elem]
176
+
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
180
+ # raised.
181
+ #
182
+ #
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
+ def peek: () -> Elem
195
+
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.
202
+ #
203
+ #
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
+ def peek_values: () -> ::Array[Elem]
222
+
223
+ # Rewinds the enumeration sequence to the beginning.
224
+ #
225
+ # If the enclosed object responds to a “rewind” method, it is called.
226
+ def rewind: () -> self
227
+
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
+ # ```
236
+ def size: () -> (Integer | Float)?
237
+
238
+ def with_index: (?Integer offset) { (Elem arg0, Integer arg1) -> untyped } -> Return
239
+ | (?Integer offset) -> ::Enumerator[[ Elem, Integer ], Return]
240
+
241
+ def with_object: [U] (U arg0) { (Elem arg0, U arg1) -> untyped } -> U
242
+ | [U] (U arg0) -> ::Enumerator[[ Elem, U ], Return]
243
+ end
244
+
245
+ class Enumerator::Generator[out Elem, out Return] < Object
246
+ include Enumerable[Elem, Return]
247
+ end
248
+
249
+ class Enumerator::Lazy[out Elem, out Return] < Enumerator[Elem, Return]
250
+ end
251
+
252
+ class Enumerator::Yielder < Object
253
+ def <<: (*untyped arg0) -> void
254
+
255
+ def yield: (*untyped arg0) -> void
256
+ end
257
+
258
+ class Enumerator::Chain[out Elem, out Return] < Object
259
+ include Enumerable[Elem, Return]
260
+ end