rbs 0.13.1 → 0.14.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 +1 -1
- data/.gitignore +0 -1
- data/CHANGELOG.md +7 -2
- data/Gemfile +3 -0
- data/README.md +8 -2
- data/Steepfile +1 -0
- data/bin/annotate-with-rdoc +1 -1
- data/bin/setup +0 -2
- data/docs/CONTRIBUTING.md +1 -0
- data/goodcheck.yml +22 -5
- data/lib/rbs/ast/comment.rb +1 -1
- data/lib/rbs/definition_builder.rb +4 -5
- data/lib/rbs/environment.rb +1 -1
- data/lib/rbs/namespace.rb +1 -1
- data/lib/rbs/parser.rb +3146 -0
- data/lib/rbs/parser.y +7 -2
- data/lib/rbs/test/setup_helper.rb +4 -4
- data/lib/rbs/test/type_check.rb +2 -2
- data/lib/rbs/type_name.rb +1 -1
- data/lib/rbs/variance_calculator.rb +1 -1
- data/lib/rbs/version.rb +1 -1
- data/lib/rbs/writer.rb +1 -1
- data/sig/constant.rbs +2 -2
- data/sig/constant_table.rbs +10 -10
- data/sig/declarations.rbs +1 -1
- data/sig/definition.rbs +1 -1
- data/sig/namespace.rbs +3 -3
- data/sig/parser.rbs +25 -0
- data/sig/substitution.rbs +3 -3
- data/sig/typename.rbs +1 -1
- data/sig/types.rbs +1 -1
- data/sig/writer.rbs +15 -15
- data/stdlib/benchmark/benchmark.rbs +2 -2
- data/stdlib/builtin/basic_object.rbs +54 -54
- data/stdlib/builtin/binding.rbs +42 -42
- data/stdlib/builtin/class.rbs +33 -33
- data/stdlib/builtin/complex.rbs +90 -90
- data/stdlib/builtin/encoding.rbs +33 -33
- data/stdlib/builtin/enumerable.rbs +32 -32
- data/stdlib/builtin/enumerator.rbs +35 -35
- data/stdlib/builtin/errors.rbs +1 -1
- data/stdlib/builtin/exception.rbs +50 -50
- data/stdlib/builtin/false_class.rbs +6 -6
- data/stdlib/builtin/fiber.rbs +14 -14
- data/stdlib/builtin/fiber_error.rbs +1 -1
- data/stdlib/builtin/float.rbs +161 -161
- data/stdlib/builtin/gc.rbs +1 -1
- data/stdlib/builtin/io.rbs +83 -83
- data/stdlib/builtin/kernel.rbs +69 -69
- data/stdlib/builtin/match_data.rbs +1 -1
- data/stdlib/builtin/method.rbs +19 -19
- data/stdlib/builtin/nil_class.rbs +20 -20
- data/stdlib/builtin/numeric.rbs +101 -101
- data/stdlib/builtin/object.rbs +172 -172
- data/stdlib/builtin/proc.rbs +91 -91
- data/stdlib/builtin/range.rbs +2 -4
- data/stdlib/builtin/rational.rbs +83 -83
- data/stdlib/builtin/signal.rbs +7 -7
- data/stdlib/builtin/string.rbs +4 -4
- data/stdlib/builtin/string_io.rbs +1 -1
- data/stdlib/builtin/thread.rbs +185 -185
- data/stdlib/builtin/thread_group.rbs +2 -2
- data/stdlib/builtin/true_class.rbs +9 -9
- data/stdlib/builtin/warning.rbs +1 -1
- data/stdlib/date/date.rbs +2 -2
- data/stdlib/find/find.rbs +10 -10
- data/stdlib/pathname/pathname.rbs +1 -1
- data/stdlib/tmpdir/tmpdir.rbs +12 -12
- metadata +3 -2
data/stdlib/builtin/encoding.rbs
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
class Encoding < Object
|
2
2
|
# Returns the hash of available encoding alias and original encoding name.
|
3
|
-
#
|
3
|
+
#
|
4
4
|
# Encoding.aliases
|
5
5
|
# #=> {"BINARY"=>"ASCII-8BIT", "ASCII"=>"US-ASCII", "ANSI_X3.4-1986"=>"US-ASCII",
|
6
6
|
# "SJIS"=>"Shift_JIS", "eucJP"=>"EUC-JP", "CP932"=>"Windows-31J"}
|
@@ -9,35 +9,35 @@ class Encoding < Object
|
|
9
9
|
def self.compatible?: (untyped obj1, untyped obj2) -> Encoding?
|
10
10
|
|
11
11
|
# Returns default external encoding.
|
12
|
-
#
|
12
|
+
#
|
13
13
|
# The default external encoding is used by default for strings created
|
14
14
|
# from the following locations:
|
15
|
-
#
|
15
|
+
#
|
16
16
|
# - CSV
|
17
|
-
#
|
17
|
+
#
|
18
18
|
# - [File](https://ruby-doc.org/core-2.6.3/File.html) data read from
|
19
19
|
# disk
|
20
|
-
#
|
20
|
+
#
|
21
21
|
# - SDBM
|
22
|
-
#
|
22
|
+
#
|
23
23
|
# - StringIO
|
24
|
-
#
|
24
|
+
#
|
25
25
|
# - Zlib::GzipReader
|
26
|
-
#
|
26
|
+
#
|
27
27
|
# - Zlib::GzipWriter
|
28
|
-
#
|
28
|
+
#
|
29
29
|
# - [String\#inspect](https://ruby-doc.org/core-2.6.3/String.html#method-i-inspect)
|
30
|
-
#
|
30
|
+
#
|
31
31
|
# - [Regexp\#inspect](https://ruby-doc.org/core-2.6.3/Regexp.html#method-i-inspect)
|
32
|
-
#
|
32
|
+
#
|
33
33
|
# While strings created from these locations will have this encoding, the
|
34
34
|
# encoding may not be valid. Be sure to check
|
35
35
|
# [String\#valid\_encoding?](https://ruby-doc.org/core-2.6.3/String.html#method-i-valid_encoding-3F)
|
36
36
|
# .
|
37
|
-
#
|
37
|
+
#
|
38
38
|
# [File](https://ruby-doc.org/core-2.6.3/File.html) data written to disk
|
39
39
|
# will be transcoded to the default external encoding when written.
|
40
|
-
#
|
40
|
+
#
|
41
41
|
# The default external encoding is initialized by the locale or -E option.
|
42
42
|
def self.default_external: () -> Encoding
|
43
43
|
|
@@ -47,43 +47,43 @@ class Encoding < Object
|
|
47
47
|
# Returns default internal encoding. Strings will be transcoded to the
|
48
48
|
# default internal encoding in the following places if the default
|
49
49
|
# internal encoding is not nil:
|
50
|
-
#
|
50
|
+
#
|
51
51
|
# - CSV
|
52
|
-
#
|
52
|
+
#
|
53
53
|
# - Etc.sysconfdir and Etc.systmpdir
|
54
|
-
#
|
54
|
+
#
|
55
55
|
# - [File](https://ruby-doc.org/core-2.6.3/File.html) data read from
|
56
56
|
# disk
|
57
|
-
#
|
57
|
+
#
|
58
58
|
# - [File](https://ruby-doc.org/core-2.6.3/File.html) names from
|
59
59
|
# [Dir](https://ruby-doc.org/core-2.6.3/Dir.html)
|
60
|
-
#
|
60
|
+
#
|
61
61
|
# - [Integer\#chr](https://ruby-doc.org/core-2.6.3/Integer.html#method-i-chr)
|
62
|
-
#
|
62
|
+
#
|
63
63
|
# - [String\#inspect](https://ruby-doc.org/core-2.6.3/String.html#method-i-inspect)
|
64
64
|
# and
|
65
65
|
# [Regexp\#inspect](https://ruby-doc.org/core-2.6.3/Regexp.html#method-i-inspect)
|
66
|
-
#
|
66
|
+
#
|
67
67
|
# - Strings returned from Readline
|
68
|
-
#
|
68
|
+
#
|
69
69
|
# - Strings returned from SDBM
|
70
|
-
#
|
70
|
+
#
|
71
71
|
# - [Time\#zone](https://ruby-doc.org/core-2.6.3/Time.html#method-i-zone)
|
72
|
-
#
|
72
|
+
#
|
73
73
|
# - Values from [ENV](https://ruby-doc.org/core-2.6.3/ENV.html)
|
74
|
-
#
|
74
|
+
#
|
75
75
|
# - Values in ARGV including $PROGRAM\_NAME
|
76
|
-
#
|
76
|
+
#
|
77
77
|
# Additionally
|
78
78
|
# [String\#encode](https://ruby-doc.org/core-2.6.3/String.html#method-i-encode)
|
79
79
|
# and
|
80
80
|
# [String\#encode\!](https://ruby-doc.org/core-2.6.3/String.html#method-i-encode-21)
|
81
81
|
# use the default internal encoding if no encoding is given.
|
82
|
-
#
|
82
|
+
#
|
83
83
|
# The locale encoding (\_\_ENCODING\_\_), not
|
84
84
|
# [::default\_internal](Encoding.downloaded.ruby_doc#method-c-default_internal)
|
85
85
|
# , is used as the encoding of created strings.
|
86
|
-
#
|
86
|
+
#
|
87
87
|
# [::default\_internal](Encoding.downloaded.ruby_doc#method-c-default_internal)
|
88
88
|
# is initialized by the source file's internal\_encoding or -E option.
|
89
89
|
def self.default_internal: () -> Encoding?
|
@@ -97,7 +97,7 @@ class Encoding < Object
|
|
97
97
|
def self.list: () -> ::Array[Encoding]
|
98
98
|
|
99
99
|
# Returns the list of available encoding names.
|
100
|
-
#
|
100
|
+
#
|
101
101
|
# Encoding.name_list
|
102
102
|
# #=> ["US-ASCII", "ASCII-8BIT", "UTF-8",
|
103
103
|
# "ISO-8859-1", "Shift_JIS", "EUC-JP",
|
@@ -106,7 +106,7 @@ class Encoding < Object
|
|
106
106
|
def self.name_list: () -> ::Array[String]
|
107
107
|
|
108
108
|
# Returns whether ASCII-compatible or not.
|
109
|
-
#
|
109
|
+
#
|
110
110
|
# ```ruby
|
111
111
|
# Encoding::UTF_8.ascii_compatible? #=> true
|
112
112
|
# Encoding::UTF_16BE.ascii_compatible? #=> false
|
@@ -116,7 +116,7 @@ class Encoding < Object
|
|
116
116
|
# Returns true for dummy encodings. A dummy encoding is an encoding for
|
117
117
|
# which character handling is not properly implemented. It is used for
|
118
118
|
# stateful encodings.
|
119
|
-
#
|
119
|
+
#
|
120
120
|
# ```ruby
|
121
121
|
# Encoding::ISO_2022_JP.dummy? #=> true
|
122
122
|
# Encoding::UTF_8.dummy? #=> false
|
@@ -126,14 +126,14 @@ class Encoding < Object
|
|
126
126
|
def inspect: () -> String
|
127
127
|
|
128
128
|
# Returns the name of the encoding.
|
129
|
-
#
|
129
|
+
#
|
130
130
|
# ```ruby
|
131
131
|
# Encoding::UTF_8.name #=> "UTF-8"
|
132
132
|
# ```
|
133
133
|
def name: () -> String
|
134
134
|
|
135
135
|
# Returns the list of name and aliases of the encoding.
|
136
|
-
#
|
136
|
+
#
|
137
137
|
# ```ruby
|
138
138
|
# Encoding::WINDOWS_31J.names #=> ["Windows-31J", "CP932", "csWindows31J"]
|
139
139
|
# ```
|
@@ -142,7 +142,7 @@ class Encoding < Object
|
|
142
142
|
def replicate: (String name) -> Encoding
|
143
143
|
|
144
144
|
# Returns the name of the encoding.
|
145
|
-
#
|
145
|
+
#
|
146
146
|
# ```ruby
|
147
147
|
# Encoding::UTF_8.name #=> "UTF-8"
|
148
148
|
# ```
|
@@ -11,10 +11,10 @@ module Enumerable[unchecked out Elem, out Return]: _Each[Elem, Return]
|
|
11
11
|
# block is not given, Ruby adds an implicit block of `{ |obj| obj }` which
|
12
12
|
# will cause [all?](Enumerable.downloaded.ruby_doc#method-i-all-3F) to
|
13
13
|
# return `true` when none of the collection members are `false` or `nil` .
|
14
|
-
#
|
14
|
+
#
|
15
15
|
# If instead a pattern is supplied, the method returns whether `pattern
|
16
16
|
# === element` for every collection member.
|
17
|
-
#
|
17
|
+
#
|
18
18
|
# %w[ant bear cat].all? { |word| word.length >= 3 } #=> true
|
19
19
|
# %w[ant bear cat].all? { |word| word.length >= 4 } #=> false
|
20
20
|
# %w[ant bear cat].all?(/t/) #=> false
|
@@ -30,10 +30,10 @@ module Enumerable[unchecked out Elem, out Return]: _Each[Elem, Return]
|
|
30
30
|
# |obj| obj }` that will cause
|
31
31
|
# [any?](Enumerable.downloaded.ruby_doc#method-i-any-3F) to return `true`
|
32
32
|
# if at least one of the collection members is not `false` or `nil` .
|
33
|
-
#
|
33
|
+
#
|
34
34
|
# If instead a pattern is supplied, the method returns whether `pattern
|
35
35
|
# === element` for any collection member.
|
36
|
-
#
|
36
|
+
#
|
37
37
|
# ```ruby
|
38
38
|
# %w[ant bear cat].any? { |word| word.length >= 3 } #=> true
|
39
39
|
# %w[ant bear cat].any? { |word| word.length >= 4 } #=> true
|
@@ -54,7 +54,7 @@ module Enumerable[unchecked out Elem, out Return]: _Each[Elem, Return]
|
|
54
54
|
# argument is given, the number of items in `enum` that are equal to
|
55
55
|
# `item` are counted. If a block is given, it counts the number of
|
56
56
|
# elements yielding a true value.
|
57
|
-
#
|
57
|
+
#
|
58
58
|
# ```ruby
|
59
59
|
# ary = [1, 2, 4, 2]
|
60
60
|
# ary.count #=> 4
|
@@ -86,11 +86,11 @@ module Enumerable[unchecked out Elem, out Return]: _Each[Elem, Return]
|
|
86
86
|
| [U] (U arg0) -> ::Enumerator[[ Elem, U ], Return]
|
87
87
|
|
88
88
|
# Returns an array containing the items in *enum* .
|
89
|
-
#
|
89
|
+
#
|
90
90
|
# ```ruby
|
91
91
|
# (1..7).to_a #=> [1, 2, 3, 4, 5, 6, 7]
|
92
92
|
# { 'a'=>1, 'b'=>2, 'c'=>3 }.to_a #=> [["a", 1], ["b", 2], ["c", 3]]
|
93
|
-
#
|
93
|
+
#
|
94
94
|
# require 'prime'
|
95
95
|
# Prime.entries 10 #=> [2, 3, 5, 7]
|
96
96
|
# ```
|
@@ -109,7 +109,7 @@ module Enumerable[unchecked out Elem, out Return]: _Each[Elem, Return]
|
|
109
109
|
# Returns the first element, or the first `n` elements, of the enumerable.
|
110
110
|
# If the enumerable is empty, the first form returns `nil`, and the
|
111
111
|
# second form returns an empty array.
|
112
|
-
#
|
112
|
+
#
|
113
113
|
# ```ruby
|
114
114
|
# %w[foo bar baz].first #=> "foo"
|
115
115
|
# %w[foo bar baz].first(2) #=> ["foo", "bar"]
|
@@ -139,16 +139,16 @@ module Enumerable[unchecked out Elem, out Return]: _Each[Elem, Return]
|
|
139
139
|
# Returns the object in *enum* with the maximum value. The first form
|
140
140
|
# assumes all objects implement `Comparable` ; the second uses the block
|
141
141
|
# to return *a \<=\> b* .
|
142
|
-
#
|
142
|
+
#
|
143
143
|
# ```ruby
|
144
144
|
# a = %w(albatross dog horse)
|
145
145
|
# a.max #=> "horse"
|
146
146
|
# a.max { |a, b| a.length <=> b.length } #=> "albatross"
|
147
147
|
# ```
|
148
|
-
#
|
148
|
+
#
|
149
149
|
# If the `n` argument is given, maximum `n` elements are returned as an
|
150
150
|
# array, sorted in descending order.
|
151
|
-
#
|
151
|
+
#
|
152
152
|
# ```ruby
|
153
153
|
# a = %w[albatross dog horse]
|
154
154
|
# a.max(2) #=> ["horse", "dog"]
|
@@ -168,16 +168,16 @@ module Enumerable[unchecked out Elem, out Return]: _Each[Elem, Return]
|
|
168
168
|
# Returns the object in *enum* with the minimum value. The first form
|
169
169
|
# assumes all objects implement `Comparable` ; the second uses the block
|
170
170
|
# to return *a \<=\> b* .
|
171
|
-
#
|
171
|
+
#
|
172
172
|
# ```ruby
|
173
173
|
# a = %w(albatross dog horse)
|
174
174
|
# a.min #=> "albatross"
|
175
175
|
# a.min { |a, b| a.length <=> b.length } #=> "dog"
|
176
176
|
# ```
|
177
|
-
#
|
177
|
+
#
|
178
178
|
# If the `n` argument is given, minimum `n` elements are returned as a
|
179
179
|
# sorted array.
|
180
|
-
#
|
180
|
+
#
|
181
181
|
# ```ruby
|
182
182
|
# a = %w[albatross dog horse]
|
183
183
|
# a.min(2) #=> ["albatross", "dog"]
|
@@ -197,7 +197,7 @@ module Enumerable[unchecked out Elem, out Return]: _Each[Elem, Return]
|
|
197
197
|
# Returns a two element array which contains the minimum and the maximum
|
198
198
|
# value in the enumerable. The first form assumes all objects implement
|
199
199
|
# `Comparable` ; the second uses the block to return *a \<=\> b* .
|
200
|
-
#
|
200
|
+
#
|
201
201
|
# ```ruby
|
202
202
|
# a = %w(albatross dog horse)
|
203
203
|
# a.minmax #=> ["albatross", "horse"]
|
@@ -213,10 +213,10 @@ module Enumerable[unchecked out Elem, out Return]: _Each[Elem, Return]
|
|
213
213
|
# returns `true` if the block never returns `true` for all elements. If
|
214
214
|
# the block is not given, `none?` will return `true` only if none of the
|
215
215
|
# collection members is true.
|
216
|
-
#
|
216
|
+
#
|
217
217
|
# If instead a pattern is supplied, the method returns whether `pattern
|
218
218
|
# === element` for none of the collection members.
|
219
|
-
#
|
219
|
+
#
|
220
220
|
# ```ruby
|
221
221
|
# %w{ant bear cat}.none? { |word| word.length == 5 } #=> true
|
222
222
|
# %w{ant bear cat}.none? { |word| word.length >= 4 } #=> false
|
@@ -234,10 +234,10 @@ module Enumerable[unchecked out Elem, out Return]: _Each[Elem, Return]
|
|
234
234
|
# returns `true` if the block returns `true` exactly once. If the block is
|
235
235
|
# not given, `one?` will return `true` only if exactly one of the
|
236
236
|
# collection members is true.
|
237
|
-
#
|
237
|
+
#
|
238
238
|
# If instead a pattern is supplied, the method returns whether `pattern
|
239
239
|
# === element` for exactly one collection member.
|
240
|
-
#
|
240
|
+
#
|
241
241
|
# ```ruby
|
242
242
|
# %w{ant bear cat}.one? { |word| word.length == 4 } #=> true
|
243
243
|
# %w{ant bear cat}.one? { |word| word.length > 4 } #=> false
|
@@ -261,22 +261,22 @@ module Enumerable[unchecked out Elem, out Return]: _Each[Elem, Return]
|
|
261
261
|
| () -> ::Enumerator[Elem, Return]
|
262
262
|
|
263
263
|
# Returns an array containing the items in *enum* sorted.
|
264
|
-
#
|
264
|
+
#
|
265
265
|
# Comparisons for the sort will be done using the items’ own `<=>`
|
266
266
|
# operator or using an optional code block.
|
267
|
-
#
|
267
|
+
#
|
268
268
|
# The block must implement a comparison between `a` and `b` and return an
|
269
269
|
# integer less than 0 when `b` follows `a`, `0` when `a` and `b` are
|
270
270
|
# equivalent, or an integer greater than 0 when `a` follows `b` .
|
271
|
-
#
|
271
|
+
#
|
272
272
|
# The result is not guaranteed to be stable. When the comparison of two
|
273
273
|
# elements returns `0`, the order of the elements is unpredictable.
|
274
|
-
#
|
274
|
+
#
|
275
275
|
# ```ruby
|
276
276
|
# %w(rhea kea flea).sort #=> ["flea", "kea", "rhea"]
|
277
277
|
# (1..10).sort { |a, b| b <=> a } #=> [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
|
278
278
|
# ```
|
279
|
-
#
|
279
|
+
#
|
280
280
|
# See also [\#sort\_by](Enumerable.downloaded.ruby_doc#method-i-sort_by).
|
281
281
|
# It implements a Schwartzian transform which is useful when key
|
282
282
|
# computation or comparison is expensive.
|
@@ -294,13 +294,13 @@ module Enumerable[unchecked out Elem, out Return]: _Each[Elem, Return]
|
|
294
294
|
# Implemented in C++
|
295
295
|
# Returns the result of interpreting *enum* as a list of `[key, value]`
|
296
296
|
# pairs.
|
297
|
-
#
|
297
|
+
#
|
298
298
|
# %i[hello world].each_with_index.to_h
|
299
299
|
# # => {:hello => 0, :world => 1}
|
300
|
-
#
|
300
|
+
#
|
301
301
|
# If a block is given, the results of the block on each element of the
|
302
302
|
# enum will be used as pairs.
|
303
|
-
#
|
303
|
+
#
|
304
304
|
# ```ruby
|
305
305
|
# (1..5).to_h {|x| [x, x ** 2]}
|
306
306
|
# #=> {1=>1, 2=>4, 3=>9, 4=>16, 5=>25}
|
@@ -324,11 +324,11 @@ module Enumerable[unchecked out Elem, out Return]: _Each[Elem, Return]
|
|
324
324
|
alias reduce inject
|
325
325
|
|
326
326
|
# Returns an array containing the items in *enum* .
|
327
|
-
#
|
327
|
+
#
|
328
328
|
# ```ruby
|
329
329
|
# (1..7).to_a #=> [1, 2, 3, 4, 5, 6, 7]
|
330
330
|
# { 'a'=>1, 'b'=>2, 'c'=>3 }.to_a #=> [["a", 1], ["b", 2], ["c", 3]]
|
331
|
-
#
|
331
|
+
#
|
332
332
|
# require 'prime'
|
333
333
|
# Prime.entries 10 #=> [2, 3, 5, 7]
|
334
334
|
# ```
|
@@ -342,10 +342,10 @@ module Enumerable[unchecked out Elem, out Return]: _Each[Elem, Return]
|
|
342
342
|
# [\#drop\_while](Enumerable.downloaded.ruby_doc#method-i-drop_while)
|
343
343
|
# enumerate values only on an as-needed basis. However, if a block is
|
344
344
|
# given to zip, values are enumerated immediately.
|
345
|
-
#
|
346
|
-
#
|
345
|
+
#
|
346
|
+
#
|
347
347
|
# The following program finds pythagorean triples:
|
348
|
-
#
|
348
|
+
#
|
349
349
|
# ```ruby
|
350
350
|
# def pythagorean_triples
|
351
351
|
# (1..Float::INFINITY).lazy.flat_map {|z|
|
@@ -1,59 +1,59 @@
|
|
1
1
|
# A class which allows both internal and external iteration.
|
2
|
-
#
|
2
|
+
#
|
3
3
|
# An [Enumerator](Enumerator) can be created by the
|
4
4
|
# following methods.
|
5
|
-
#
|
5
|
+
#
|
6
6
|
# - Kernel\#to\_enum
|
7
|
-
#
|
7
|
+
#
|
8
8
|
# - Kernel\#enum\_for
|
9
|
-
#
|
9
|
+
#
|
10
10
|
# - [::new](Enumerator#method-c-new)
|
11
|
-
#
|
11
|
+
#
|
12
12
|
# Most methods have two forms: a block form where the contents are
|
13
13
|
# evaluated for each item in the enumeration, and a non-block form which
|
14
14
|
# returns a new [Enumerator](Enumerator) wrapping the
|
15
15
|
# iteration.
|
16
|
-
#
|
16
|
+
#
|
17
17
|
# ```ruby
|
18
18
|
# enumerator = %w(one two three).each
|
19
19
|
# puts enumerator.class # => Enumerator
|
20
|
-
#
|
20
|
+
#
|
21
21
|
# enumerator.each_with_object("foo") do |item, obj|
|
22
22
|
# puts "#{obj}: #{item}"
|
23
23
|
# end
|
24
|
-
#
|
24
|
+
#
|
25
25
|
# # foo: one
|
26
26
|
# # foo: two
|
27
27
|
# # foo: three
|
28
|
-
#
|
28
|
+
#
|
29
29
|
# enum_with_obj = enumerator.each_with_object("foo")
|
30
30
|
# puts enum_with_obj.class # => Enumerator
|
31
|
-
#
|
31
|
+
#
|
32
32
|
# enum_with_obj.each do |item, obj|
|
33
33
|
# puts "#{obj}: #{item}"
|
34
34
|
# end
|
35
|
-
#
|
35
|
+
#
|
36
36
|
# # foo: one
|
37
37
|
# # foo: two
|
38
38
|
# # foo: three
|
39
39
|
# ```
|
40
|
-
#
|
40
|
+
#
|
41
41
|
# This allows you to chain Enumerators together. For example, you can map
|
42
42
|
# a list's elements to strings containing the index and the element as a
|
43
43
|
# string via:
|
44
|
-
#
|
44
|
+
#
|
45
45
|
# ```ruby
|
46
46
|
# puts %w[foo bar baz].map.with_index { |w, i| "#{i}:#{w}" }
|
47
47
|
# # => ["0:foo", "1:bar", "2:baz"]
|
48
48
|
# ```
|
49
|
-
#
|
49
|
+
#
|
50
50
|
# An [Enumerator](Enumerator) can also be used as an
|
51
51
|
# external iterator. For example,
|
52
52
|
# [\#next](Enumerator#method-i-next) returns the next
|
53
53
|
# value of the iterator or raises
|
54
54
|
# [StopIteration](https://ruby-doc.org/core-2.6.3/StopIteration.html) if
|
55
55
|
# the [Enumerator](Enumerator) is at the end.
|
56
|
-
#
|
56
|
+
#
|
57
57
|
# ```ruby
|
58
58
|
# e = [1,2,3].each # returns an enumerator object.
|
59
59
|
# puts e.next # => 1
|
@@ -61,9 +61,9 @@
|
|
61
61
|
# puts e.next # => 3
|
62
62
|
# puts e.next # raises StopIteration
|
63
63
|
# ```
|
64
|
-
#
|
64
|
+
#
|
65
65
|
# You can use this to implement an internal iterator as follows:
|
66
|
-
#
|
66
|
+
#
|
67
67
|
# ```ruby
|
68
68
|
# def ext_each(e)
|
69
69
|
# while true
|
@@ -76,20 +76,20 @@
|
|
76
76
|
# e.feed y
|
77
77
|
# end
|
78
78
|
# end
|
79
|
-
#
|
79
|
+
#
|
80
80
|
# o = Object.new
|
81
|
-
#
|
81
|
+
#
|
82
82
|
# def o.each
|
83
83
|
# puts yield
|
84
84
|
# puts yield(1)
|
85
85
|
# puts yield(1, 2)
|
86
86
|
# 3
|
87
87
|
# end
|
88
|
-
#
|
88
|
+
#
|
89
89
|
# # use o.each as an internal iterator directly.
|
90
90
|
# puts o.each {|*x| puts x; [:b, *x] }
|
91
91
|
# # => [], [:b], [1], [:b, 1], [1, 2], [:b, 1, 2], 3
|
92
|
-
#
|
92
|
+
#
|
93
93
|
# # convert o.each to an external iterator for
|
94
94
|
# # implementing an internal iterator.
|
95
95
|
# puts ext_each(o.to_enum) {|*x| puts x; [:b, *x] }
|
@@ -112,8 +112,8 @@ class Enumerator[unchecked out Elem, out Return] < Object
|
|
112
112
|
# position forward. When the position reached at the end,
|
113
113
|
# [StopIteration](https://ruby-doc.org/core-2.6.3/StopIteration.html) is
|
114
114
|
# raised.
|
115
|
-
#
|
116
|
-
#
|
115
|
+
#
|
116
|
+
#
|
117
117
|
# ```ruby
|
118
118
|
# a = [1,2,3]
|
119
119
|
# e = a.to_enum
|
@@ -122,7 +122,7 @@ class Enumerator[unchecked out Elem, out Return] < Object
|
|
122
122
|
# p e.next #=> 3
|
123
123
|
# p e.next #raises StopIteration
|
124
124
|
# ```
|
125
|
-
#
|
125
|
+
#
|
126
126
|
# Note that enumeration sequence by `next` does not affect other
|
127
127
|
# non-external enumeration methods, unless the underlying iteration
|
128
128
|
# methods itself has side-effect, e.g.
|
@@ -134,10 +134,10 @@ class Enumerator[unchecked out Elem, out Return] < Object
|
|
134
134
|
# internal position forward. When the position reached at the end,
|
135
135
|
# [StopIteration](https://ruby-doc.org/core-2.6.3/StopIteration.html) is
|
136
136
|
# raised.
|
137
|
-
#
|
137
|
+
#
|
138
138
|
# This method can be used to distinguish `yield` and `yield nil` .
|
139
|
-
#
|
140
|
-
#
|
139
|
+
#
|
140
|
+
#
|
141
141
|
# ```ruby
|
142
142
|
# o = Object.new
|
143
143
|
# def o.each
|
@@ -159,7 +159,7 @@ class Enumerator[unchecked out Elem, out Return] < Object
|
|
159
159
|
# p e.next
|
160
160
|
# p e.next
|
161
161
|
# p e.next
|
162
|
-
#
|
162
|
+
#
|
163
163
|
# ## yield args next_values next
|
164
164
|
# # yield [] nil
|
165
165
|
# # yield 1 [1] 1
|
@@ -167,7 +167,7 @@ class Enumerator[unchecked out Elem, out Return] < Object
|
|
167
167
|
# # yield nil [nil] nil
|
168
168
|
# # yield [1, 2] [[1, 2]] [1, 2]
|
169
169
|
# ```
|
170
|
-
#
|
170
|
+
#
|
171
171
|
# Note that `next_values` does not affect other non-external enumeration
|
172
172
|
# methods unless underlying iteration method itself has side-effect, e.g.
|
173
173
|
# [IO\#each\_line](https://ruby-doc.org/core-2.6.3/IO.html#method-i-each_line)
|
@@ -178,8 +178,8 @@ class Enumerator[unchecked out Elem, out Return] < Object
|
|
178
178
|
# position forward. If the position is already at the end,
|
179
179
|
# [StopIteration](https://ruby-doc.org/core-2.6.3/StopIteration.html) is
|
180
180
|
# raised.
|
181
|
-
#
|
182
|
-
#
|
181
|
+
#
|
182
|
+
#
|
183
183
|
# ```ruby
|
184
184
|
# a = [1,2,3]
|
185
185
|
# e = a.to_enum
|
@@ -199,8 +199,8 @@ class Enumerator[unchecked out Elem, out Return] < Object
|
|
199
199
|
# already at the end,
|
200
200
|
# [StopIteration](https://ruby-doc.org/core-2.6.3/StopIteration.html) is
|
201
201
|
# raised.
|
202
|
-
#
|
203
|
-
#
|
202
|
+
#
|
203
|
+
#
|
204
204
|
# ```ruby
|
205
205
|
# o = Object.new
|
206
206
|
# def o.each
|
@@ -221,13 +221,13 @@ class Enumerator[unchecked out Elem, out Return] < Object
|
|
221
221
|
def peek_values: () -> ::Array[Elem]
|
222
222
|
|
223
223
|
# Rewinds the enumeration sequence to the beginning.
|
224
|
-
#
|
224
|
+
#
|
225
225
|
# If the enclosed object responds to a “rewind” method, it is called.
|
226
226
|
def rewind: () -> self
|
227
227
|
|
228
228
|
# Returns the size of the enumerator, or `nil` if it can’t be calculated
|
229
229
|
# lazily.
|
230
|
-
#
|
230
|
+
#
|
231
231
|
# ```ruby
|
232
232
|
# (1..100).to_a.permutation(4).size # => 94109400
|
233
233
|
# loop.size # => Float::INFINITY
|