rbs 3.0.0.dev.2 → 3.0.0.dev.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (112) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/comments.yml +2 -1
  3. data/.github/workflows/ruby.yml +4 -0
  4. data/Gemfile.lock +11 -11
  5. data/Rakefile +2 -2
  6. data/Steepfile +1 -1
  7. data/core/array.rbs +573 -423
  8. data/core/basic_object.rbs +11 -39
  9. data/core/binding.rbs +1 -1
  10. data/core/builtin.rbs +8 -0
  11. data/core/class.rbs +37 -0
  12. data/core/comparable.rbs +7 -18
  13. data/core/complex.rbs +2 -2
  14. data/core/data.rbs +419 -0
  15. data/core/dir.rbs +52 -104
  16. data/core/encoding.rbs +22 -181
  17. data/core/enumerable.rbs +212 -175
  18. data/core/enumerator/product.rbs +96 -0
  19. data/core/enumerator.rbs +57 -8
  20. data/core/errors.rbs +8 -2
  21. data/core/exception.rbs +41 -0
  22. data/core/fiber.rbs +95 -12
  23. data/core/file.rbs +840 -275
  24. data/core/file_test.rbs +34 -19
  25. data/core/float.rbs +40 -96
  26. data/core/gc.rbs +15 -3
  27. data/core/hash.rbs +113 -175
  28. data/core/integer.rbs +85 -145
  29. data/core/io/buffer.rbs +187 -60
  30. data/core/io/wait.rbs +28 -16
  31. data/core/io.rbs +1859 -1389
  32. data/core/kernel.rbs +525 -961
  33. data/core/match_data.rbs +306 -142
  34. data/core/math.rbs +506 -234
  35. data/core/method.rbs +0 -24
  36. data/core/module.rbs +110 -17
  37. data/core/nil_class.rbs +2 -0
  38. data/core/numeric.rbs +76 -144
  39. data/core/object.rbs +88 -212
  40. data/core/proc.rbs +17 -5
  41. data/core/process.rbs +22 -5
  42. data/core/ractor.rbs +1 -1
  43. data/core/random.rbs +20 -3
  44. data/core/range.rbs +91 -89
  45. data/core/rational.rbs +2 -3
  46. data/core/rbs/unnamed/argf.rbs +177 -120
  47. data/core/rbs/unnamed/env_class.rbs +89 -163
  48. data/core/rbs/unnamed/random.rbs +36 -12
  49. data/core/refinement.rbs +8 -0
  50. data/core/regexp.rbs +462 -272
  51. data/core/ruby_vm.rbs +210 -0
  52. data/{stdlib/set/0 → core}/set.rbs +43 -47
  53. data/core/string.rbs +1403 -1332
  54. data/core/string_io.rbs +191 -107
  55. data/core/struct.rbs +67 -63
  56. data/core/symbol.rbs +187 -201
  57. data/core/thread.rbs +40 -35
  58. data/core/time.rbs +902 -826
  59. data/core/trace_point.rbs +55 -6
  60. data/core/unbound_method.rbs +48 -24
  61. data/docs/collection.md +4 -0
  62. data/docs/syntax.md +55 -0
  63. data/ext/rbs_extension/parser.c +5 -6
  64. data/lib/rbs/cli.rb +6 -1
  65. data/lib/rbs/collection/cleaner.rb +8 -1
  66. data/lib/rbs/collection/config/lockfile.rb +3 -1
  67. data/lib/rbs/collection/config/lockfile_generator.rb +16 -14
  68. data/lib/rbs/collection/config.rb +1 -1
  69. data/lib/rbs/collection/sources/git.rb +9 -2
  70. data/lib/rbs/collection/sources/local.rb +79 -0
  71. data/lib/rbs/collection/sources.rb +8 -1
  72. data/lib/rbs/environment.rb +6 -5
  73. data/lib/rbs/environment_loader.rb +3 -2
  74. data/lib/rbs/errors.rb +18 -0
  75. data/lib/rbs/locator.rb +26 -7
  76. data/lib/rbs/sorter.rb +2 -2
  77. data/lib/rbs/version.rb +1 -1
  78. data/sig/collection/sources.rbs +32 -3
  79. data/sig/environment.rbs +2 -3
  80. data/sig/locator.rbs +14 -2
  81. data/sig/shims/{abstract_syntax_tree.rbs → _abstract_syntax_tree.rbs} +0 -0
  82. data/stdlib/bigdecimal/0/big_decimal.rbs +16 -13
  83. data/stdlib/cgi/0/core.rbs +16 -0
  84. data/stdlib/coverage/0/coverage.rbs +50 -8
  85. data/stdlib/csv/0/csv.rbs +1 -1
  86. data/stdlib/date/0/date.rbs +856 -726
  87. data/stdlib/date/0/date_time.rbs +83 -210
  88. data/stdlib/erb/0/erb.rbs +13 -36
  89. data/stdlib/etc/0/etc.rbs +127 -20
  90. data/stdlib/fileutils/0/fileutils.rbs +1290 -381
  91. data/stdlib/logger/0/logger.rbs +466 -316
  92. data/stdlib/net-http/0/net-http.rbs +2211 -534
  93. data/stdlib/nkf/0/nkf.rbs +5 -5
  94. data/stdlib/objspace/0/objspace.rbs +31 -14
  95. data/stdlib/openssl/0/openssl.rbs +11 -7
  96. data/stdlib/optparse/0/optparse.rbs +20 -17
  97. data/stdlib/pathname/0/pathname.rbs +21 -4
  98. data/stdlib/pstore/0/pstore.rbs +378 -154
  99. data/stdlib/pty/0/pty.rbs +24 -8
  100. data/stdlib/ripper/0/ripper.rbs +1650 -0
  101. data/stdlib/socket/0/addrinfo.rbs +9 -15
  102. data/stdlib/socket/0/socket.rbs +36 -3
  103. data/stdlib/strscan/0/string_scanner.rbs +7 -5
  104. data/stdlib/tempfile/0/tempfile.rbs +104 -44
  105. data/stdlib/time/0/time.rbs +2 -2
  106. data/stdlib/uri/0/file.rbs +5 -0
  107. data/stdlib/uri/0/generic.rbs +2 -2
  108. data/stdlib/yaml/0/yaml.rbs +2 -2
  109. data/stdlib/zlib/0/zlib.rbs +1 -1
  110. metadata +8 -6
  111. data/core/deprecated.rbs +0 -9
  112. data/sig/shims/ripper.rbs +0 -8
data/core/enumerable.rbs CHANGED
@@ -2,12 +2,13 @@
2
2
  # ## What's Here
3
3
  #
4
4
  # Module Enumerable provides methods that are useful to a collection class for:
5
- # * [Querying](#module-Enumerable-label-Methods+for+Querying)
6
- # * [Fetching](#module-Enumerable-label-Methods+for+Fetching)
7
- # * [Searching](#module-Enumerable-label-Methods+for+Searching)
8
- # * [Sorting](#module-Enumerable-label-Methods+for+Sorting)
9
- # * [Iterating](#module-Enumerable-label-Methods+for+Iterating)
10
- # * [And more....](#module-Enumerable-label-Other+Methods)
5
+ #
6
+ # * [Querying](rdoc-ref:Enumerable@Methods+for+Querying)
7
+ # * [Fetching](rdoc-ref:Enumerable@Methods+for+Fetching)
8
+ # * [Searching](rdoc-ref:Enumerable@Methods+for+Searching)
9
+ # * [Sorting](rdoc-ref:Enumerable@Methods+for+Sorting)
10
+ # * [Iterating](rdoc-ref:Enumerable@Methods+for+Iterating)
11
+ # * [And more....](rdoc-ref:Enumerable@Other+Methods)
11
12
  #
12
13
  #
13
14
  # ### Methods for Querying
@@ -15,25 +16,20 @@
15
16
  # These methods return information about the Enumerable other than the elements
16
17
  # themselves:
17
18
  #
18
- # #include?, #member?
19
- # : Returns `true` if self == object, `false` otherwise.
20
- # #all?
21
- # : Returns `true` if all elements meet a specified criterion; `false`
19
+ # * #include?, #member?: Returns `true` if `self == object`, `false`
22
20
  # otherwise.
23
- # #any?
24
- # : Returns `true` if any element meets a specified criterion; `false`
21
+ # * #all?: Returns `true` if all elements meet a specified criterion; `false`
25
22
  # otherwise.
26
- # #none?
27
- # : Returns `true` if no element meets a specified criterion; `false`
23
+ # * #any?: Returns `true` if any element meets a specified criterion; `false`
28
24
  # otherwise.
29
- # #one?
30
- # : Returns `true` if exactly one element meets a specified criterion; `false`
25
+ # * #none?: Returns `true` if no element meets a specified criterion; `false`
31
26
  # otherwise.
32
- # #count
33
- # : Returns the count of elements, based on an argument or block criterion, if
34
- # given.
35
- # #tally
36
- # : Returns a new Hash containing the counts of occurrences of each element.
27
+ # * #one?: Returns `true` if exactly one element meets a specified criterion;
28
+ # `false` otherwise.
29
+ # * #count: Returns the count of elements, based on an argument or block
30
+ # criterion, if given.
31
+ # * #tally: Returns a new Hash containing the counts of occurrences of each
32
+ # element.
37
33
  #
38
34
  #
39
35
  # ### Methods for Fetching
@@ -41,126 +37,96 @@
41
37
  # These methods return entries from the Enumerable, without modifying it:
42
38
  #
43
39
  # *Leading, trailing, or all elements*:
44
- # #entries, #to_a
45
- # : Returns all elements.
46
- # #first
47
- # : Returns the first element or leading elements.
48
- # #take
49
- # : Returns a specified number of leading elements.
50
- # #drop
51
- # : Returns a specified number of trailing elements.
52
- # #take_while
53
- # : Returns leading elements as specified by the given block.
54
- # #drop_while
55
- # : Returns trailing elements as specified by the given block.
40
+ #
41
+ # * #entries, #to_a: Returns all elements.
42
+ # * #first: Returns the first element or leading elements.
43
+ # * #take: Returns a specified number of leading elements.
44
+ # * #drop: Returns a specified number of trailing elements.
45
+ # * #take_while: Returns leading elements as specified by the given block.
46
+ # * #drop_while: Returns trailing elements as specified by the given block.
56
47
  #
57
48
  #
58
49
  # *Minimum and maximum value elements*:
59
- # #min
60
- # : Returns the elements whose values are smallest among the elements, as
61
- # determined by `<=>` or a given block.
62
- # #max
63
- # : Returns the elements whose values are largest among the elements, as
50
+ #
51
+ # * #min: Returns the elements whose values are smallest among the elements,
52
+ # as determined by `<=>` or a given block.
53
+ # * #max: Returns the elements whose values are largest among the elements, as
64
54
  # determined by `<=>` or a given block.
65
- # #minmax
66
- # : Returns a 2-element Array containing the smallest and largest elements.
67
- # #min_by
68
- # : Returns the smallest element, as determined by the given block.
69
- # #max_by
70
- # : Returns the largest element, as determined by the given block.
71
- # #minmax_by
72
- # : Returns the smallest and largest elements, as determined by the given
73
- # block.
55
+ # * #minmax: Returns a 2-element Array containing the smallest and largest
56
+ # elements.
57
+ # * #min_by: Returns the smallest element, as determined by the given block.
58
+ # * #max_by: Returns the largest element, as determined by the given block.
59
+ # * #minmax_by: Returns the smallest and largest elements, as determined by
60
+ # the given block.
74
61
  #
75
62
  #
76
63
  # *Groups, slices, and partitions*:
77
- # #group_by
78
- # : Returns a Hash that partitions the elements into groups.
79
- # #partition
80
- # : Returns elements partitioned into two new Arrays, as determined by the
64
+ #
65
+ # * #group_by: Returns a Hash that partitions the elements into groups.
66
+ # * #partition: Returns elements partitioned into two new Arrays, as
67
+ # determined by the given block.
68
+ # * #slice_after: Returns a new Enumerator whose entries are a partition of
69
+ # `self`, based either on a given `object` or a given block.
70
+ # * #slice_before: Returns a new Enumerator whose entries are a partition of
71
+ # `self`, based either on a given `object` or a given block.
72
+ # * #slice_when: Returns a new Enumerator whose entries are a partition of
73
+ # `self` based on the given block.
74
+ # * #chunk: Returns elements organized into chunks as specified by the given
75
+ # block.
76
+ # * #chunk_while: Returns elements organized into chunks as specified by the
81
77
  # given block.
82
- # #slice_after
83
- # : Returns a new Enumerator whose entries are a partition of `self`, based
84
- # either on a given `object` or a given block.
85
- # #slice_before
86
- # : Returns a new Enumerator whose entries are a partition of `self`, based
87
- # either on a given `object` or a given block.
88
- # #slice_when
89
- # : Returns a new Enumerator whose entries are a partition of `self` based on
90
- # the given block.
91
- # #chunk
92
- # : Returns elements organized into chunks as specified by the given block.
93
- # #chunk_while
94
- # : Returns elements organized into chunks as specified by the given block.
95
78
  #
96
79
  #
97
80
  # ### Methods for Searching and Filtering
98
81
  #
99
- # These methods return elements that meet a specified criterion.
82
+ # These methods return elements that meet a specified criterion:
100
83
  #
101
- # #find, #detect
102
- # : Returns an element selected by the block.
103
- # #find_all, #filter, #select
104
- # : Returns elements selected by the block.
105
- # #find_index
106
- # : Returns the index of an element selected by a given object or block.
107
- # #reject
108
- # : Returns elements not rejected by the block.
109
- # #uniq
110
- # : Returns elements that are not duplicates.
84
+ # * #find, #detect: Returns an element selected by the block.
85
+ # * #find_all, #filter, #select: Returns elements selected by the block.
86
+ # * #find_index: Returns the index of an element selected by a given object or
87
+ # block.
88
+ # * #reject: Returns elements not rejected by the block.
89
+ # * #uniq: Returns elements that are not duplicates.
111
90
  #
112
91
  #
113
92
  # ### Methods for Sorting
114
93
  #
115
- # These methods return elements in sorted order.
94
+ # These methods return elements in sorted order:
116
95
  #
117
- # #sort
118
- # : Returns the elements, sorted by `<=>` or the given block.
119
- # #sort_by
120
- # : Returns the elements, sorted by the given block.
96
+ # * #sort: Returns the elements, sorted by `<=>` or the given block.
97
+ # * #sort_by: Returns the elements, sorted by the given block.
121
98
  #
122
99
  #
123
100
  # ### Methods for Iterating
124
101
  #
125
- # #each_entry
126
- # : Calls the block with each successive element (slightly different from
127
- # #each).
128
- # #each_with_index
129
- # : Calls the block with each successive element and its index.
130
- # #each_with_object
131
- # : Calls the block with each successive element and a given object.
132
- # #each_slice
133
- # : Calls the block with successive non-overlapping slices.
134
- # #each_cons
135
- # : Calls the block with successive overlapping slices. (different from
136
- # #each_slice).
137
- # #reverse_each
138
- # : Calls the block with each successive element, in reverse order.
102
+ # * #each_entry: Calls the block with each successive element (slightly
103
+ # different from #each).
104
+ # * #each_with_index: Calls the block with each successive element and its
105
+ # index.
106
+ # * #each_with_object: Calls the block with each successive element and a
107
+ # given object.
108
+ # * #each_slice: Calls the block with successive non-overlapping slices.
109
+ # * #each_cons: Calls the block with successive overlapping slices. (different
110
+ # from #each_slice).
111
+ # * #reverse_each: Calls the block with each successive element, in reverse
112
+ # order.
139
113
  #
140
114
  #
141
115
  # ### Other Methods
142
116
  #
143
- # #map, #collect
144
- # : Returns objects returned by the block.
145
- # #filter_map
146
- # : Returns truthy objects returned by the block.
147
- # #flat_map, #collect_concat
148
- # : Returns flattened objects returned by the block.
149
- # #grep
150
- # : Returns elements selected by a given object or objects returned by a given
151
- # block.
152
- # #grep_v
153
- # : Returns elements selected by a given object or objects returned by a given
117
+ # * #map, #collect: Returns objects returned by the block.
118
+ # * #filter_map: Returns truthy objects returned by the block.
119
+ # * #flat_map, #collect_concat: Returns flattened objects returned by the
154
120
  # block.
155
- # #reduce, #inject
156
- # : Returns the object formed by combining all elements.
157
- # #sum
158
- # : Returns the sum of the elements, using method +++.
159
- # #zip
160
- # : Combines each element with elements from other enumerables; returns the
161
- # n-tuples or calls the block with each.
162
- # #cycle
163
- # : Calls the block with each element, cycling repeatedly.
121
+ # * #grep: Returns elements selected by a given object or objects returned by
122
+ # a given block.
123
+ # * #grep_v: Returns elements selected by a given object or objects returned
124
+ # by a given block.
125
+ # * #reduce, #inject: Returns the object formed by combining all elements.
126
+ # * #sum: Returns the sum of the elements, using method `+`.
127
+ # * #zip: Combines each element with elements from other enumerables; returns
128
+ # the n-tuples or calls the block with each.
129
+ # * #cycle: Calls the block with each element, cycling repeatedly.
164
130
  #
165
131
  #
166
132
  # ## Usage
@@ -193,18 +159,32 @@
193
159
  # [1, 2]
194
160
  # nil
195
161
  #
196
- # ## Enumerable in Ruby Core Classes
197
- # Some Ruby classes include Enumerable:
162
+ # ## Enumerable in Ruby Classes
163
+ #
164
+ # These Ruby core classes include (or extend) Enumerable:
165
+ #
166
+ # * ARGF
198
167
  # * Array
199
168
  # * Dir
169
+ # * Enumerator
170
+ # * ENV (extends)
200
171
  # * Hash
201
172
  # * IO
202
173
  # * Range
203
- # * Set
204
174
  # * Struct
205
175
  #
176
+ #
177
+ # These Ruby standard library classes include Enumerable:
178
+ #
179
+ # * CSV
180
+ # * CSV::Table
181
+ # * CSV::Row
182
+ # * Set
183
+ #
184
+ #
206
185
  # Virtually all methods in Enumerable call method `#each` in the including
207
186
  # class:
187
+ #
208
188
  # * `Hash#each` yields the next key-value pair as a 2-element Array.
209
189
  # * `Struct#each` yields the next name-value pair as a 2-element Array.
210
190
  # * For the other classes above, `#each` yields the next object from the
@@ -212,7 +192,9 @@
212
192
  #
213
193
  #
214
194
  # ## About the Examples
195
+ #
215
196
  # The example code snippets for the Enumerable methods:
197
+ #
216
198
  # * Always show the use of one or more Array-like classes (often Array
217
199
  # itself).
218
200
  # * Sometimes show the use of a Hash-like class. For some methods, though, the
@@ -523,8 +505,10 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
523
505
  # Calls the block once for each element, passing both the element and the given
524
506
  # object:
525
507
  #
526
- # (1..4).each_with_object([]) {|i, a| a.push(i**2) } # => [1, 4, 9, 16]
527
- # h.each_with_object({}) {|element, h| k, v = *element; h[v] = k }
508
+ # (1..4).each_with_object([]) {|i, a| a.push(i**2) }
509
+ # # => [1, 4, 9, 16]
510
+ #
511
+ # {foo: 0, bar: 1, baz: 2}.each_with_object({}) {|(k, v), h| h[v] = k }
528
512
  # # => {0=>:foo, 1=>:bar, 2=>:baz}
529
513
  #
530
514
  # With no block given, returns an Enumerator.
@@ -1998,8 +1982,10 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
1998
1982
  # [:a2, :b2, :c2]
1999
1983
  # [:a3, :b3, :c3]
2000
1984
  #
2001
- def zip: [Elem2] (::Enumerable[Elem2] enum) -> ::Array[[ Elem, Elem2 | nil ]]
2002
- | [U, Elem2] (::Enumerable[Elem2]) { ([ Elem, Elem2 | nil ]) -> U } -> nil
1985
+ def zip: [Elem2] (_Each[Elem2] enum) -> Array[[ Elem, Elem2? ]]
1986
+ | (_Each[untyped], *_Each[untyped]) -> Array[Array[untyped]]
1987
+ | [Elem2] (_Each[Elem2]) { ([ Elem, Elem2? ]) -> void } -> nil
1988
+ | (_Each[untyped], *_Each[untyped]) { (Array[untyped]) -> void } -> nil
2003
1989
 
2004
1990
  # <!--
2005
1991
  # rdoc-file=enum.c
@@ -2112,83 +2098,134 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
2112
2098
 
2113
2099
  # <!--
2114
2100
  # rdoc-file=enum.c
2115
- # - chunk_while {|element, next_element| ... } -> enumerator
2101
+ # - enum.chunk_while {|elt_before, elt_after| bool } -> an_enumerator
2116
2102
  # -->
2117
- # The returned Enumerator uses the block to partition elements into arrays
2118
- # ("chunks"); it calls the block with each element and its successor; begins a
2119
- # new chunk if and only if the block returns a truthy value:
2103
+ # Creates an enumerator for each chunked elements. The beginnings of chunks are
2104
+ # defined by the block.
2120
2105
  #
2121
- # Example:
2106
+ # This method splits each chunk using adjacent elements, *elt_before* and
2107
+ # *elt_after*, in the receiver enumerator. This method split chunks between
2108
+ # *elt_before* and *elt_after* where the block returns `false`.
2122
2109
  #
2123
- # a = [1, 2, 4, 9, 10, 11, 12, 15, 16, 19, 20, 21]
2124
- # e = a.chunk_while {|i, j| j == i + 1 }
2125
- # e.each {|array| p array }
2110
+ # The block is called the length of the receiver enumerator minus one.
2126
2111
  #
2127
- # Output:
2112
+ # The result enumerator yields the chunked elements as an array. So `each`
2113
+ # method can be called as follows:
2128
2114
  #
2129
- # [1, 2]
2130
- # [4]
2131
- # [9, 10, 11, 12]
2132
- # [15, 16]
2133
- # [19, 20, 21]
2115
+ # enum.chunk_while { |elt_before, elt_after| bool }.each { |ary| ... }
2116
+ #
2117
+ # Other methods of the Enumerator class and Enumerable module, such as `to_a`,
2118
+ # `map`, etc., are also usable.
2119
+ #
2120
+ # For example, one-by-one increasing subsequence can be chunked as follows:
2121
+ #
2122
+ # a = [1,2,4,9,10,11,12,15,16,19,20,21]
2123
+ # b = a.chunk_while {|i, j| i+1 == j }
2124
+ # p b.to_a #=> [[1, 2], [4], [9, 10, 11, 12], [15, 16], [19, 20, 21]]
2125
+ # c = b.map {|a| a.length < 3 ? a : "#{a.first}-#{a.last}" }
2126
+ # p c #=> [[1, 2], [4], "9-12", [15, 16], "19-21"]
2127
+ # d = c.join(",")
2128
+ # p d #=> "1,2,4,9-12,15,16,19-21"
2129
+ #
2130
+ # Increasing (non-decreasing) subsequence can be chunked as follows:
2131
+ #
2132
+ # a = [0, 9, 2, 2, 3, 2, 7, 5, 9, 5]
2133
+ # p a.chunk_while {|i, j| i <= j }.to_a
2134
+ # #=> [[0, 9], [2, 2, 3], [2, 7], [5, 9], [5]]
2135
+ #
2136
+ # Adjacent evens and odds can be chunked as follows: (Enumerable#chunk is
2137
+ # another way to do it.)
2138
+ #
2139
+ # a = [7, 5, 9, 2, 0, 7, 9, 4, 2, 0]
2140
+ # p a.chunk_while {|i, j| i.even? == j.even? }.to_a
2141
+ # #=> [[7, 5, 9], [2, 0], [7, 9], [4, 2, 0]]
2142
+ #
2143
+ # Enumerable#slice_when does the same, except splitting when the block returns
2144
+ # `true` instead of `false`.
2134
2145
  #
2135
2146
  def chunk_while: () { (Elem elt_before, Elem elt_after) -> boolish } -> ::Enumerator[::Array[Elem], void]
2136
2147
 
2137
2148
  # <!--
2138
2149
  # rdoc-file=enum.c
2139
- # - slice_when {|element, next_element| ... } -> enumerator
2150
+ # - enum.slice_when {|elt_before, elt_after| bool } -> an_enumerator
2140
2151
  # -->
2141
- # The returned enumerator uses the block to partition elements into arrays
2142
- # ("slices"); it calls the block with each element and its successor; begins a
2143
- # new slice if and only if the block returns a truthy value:
2152
+ # Creates an enumerator for each chunked elements. The beginnings of chunks are
2153
+ # defined by the block.
2144
2154
  #
2145
- # a = [0, 1, 2, 4, 5, 6, 8, 9]
2146
- # e = a.slice_when {|i, j| j != i + 1 }
2147
- # e.each {|array| p array }
2155
+ # This method splits each chunk using adjacent elements, *elt_before* and
2156
+ # *elt_after*, in the receiver enumerator. This method split chunks between
2157
+ # *elt_before* and *elt_after* where the block returns `true`.
2148
2158
  #
2149
- # Output:
2159
+ # The block is called the length of the receiver enumerator minus one.
2160
+ #
2161
+ # The result enumerator yields the chunked elements as an array. So `each`
2162
+ # method can be called as follows:
2163
+ #
2164
+ # enum.slice_when { |elt_before, elt_after| bool }.each { |ary| ... }
2165
+ #
2166
+ # Other methods of the Enumerator class and Enumerable module, such as `to_a`,
2167
+ # `map`, etc., are also usable.
2168
+ #
2169
+ # For example, one-by-one increasing subsequence can be chunked as follows:
2170
+ #
2171
+ # a = [1,2,4,9,10,11,12,15,16,19,20,21]
2172
+ # b = a.slice_when {|i, j| i+1 != j }
2173
+ # p b.to_a #=> [[1, 2], [4], [9, 10, 11, 12], [15, 16], [19, 20, 21]]
2174
+ # c = b.map {|a| a.length < 3 ? a : "#{a.first}-#{a.last}" }
2175
+ # p c #=> [[1, 2], [4], "9-12", [15, 16], "19-21"]
2176
+ # d = c.join(",")
2177
+ # p d #=> "1,2,4,9-12,15,16,19-21"
2178
+ #
2179
+ # Near elements (threshold: 6) in sorted array can be chunked as follows:
2180
+ #
2181
+ # a = [3, 11, 14, 25, 28, 29, 29, 41, 55, 57]
2182
+ # p a.slice_when {|i, j| 6 < j - i }.to_a
2183
+ # #=> [[3], [11, 14], [25, 28, 29, 29], [41], [55, 57]]
2184
+ #
2185
+ # Increasing (non-decreasing) subsequence can be chunked as follows:
2186
+ #
2187
+ # a = [0, 9, 2, 2, 3, 2, 7, 5, 9, 5]
2188
+ # p a.slice_when {|i, j| i > j }.to_a
2189
+ # #=> [[0, 9], [2, 2, 3], [2, 7], [5, 9], [5]]
2150
2190
  #
2151
- # [0, 1, 2]
2152
- # [4, 5, 6]
2153
- # [8, 9]
2191
+ # Adjacent evens and odds can be chunked as follows: (Enumerable#chunk is
2192
+ # another way to do it.)
2193
+ #
2194
+ # a = [7, 5, 9, 2, 0, 7, 9, 4, 2, 0]
2195
+ # p a.slice_when {|i, j| i.even? != j.even? }.to_a
2196
+ # #=> [[7, 5, 9], [2, 0], [7, 9], [4, 2, 0]]
2197
+ #
2198
+ # Paragraphs (non-empty lines with trailing empty lines) can be chunked as
2199
+ # follows: (See Enumerable#chunk to ignore empty lines.)
2200
+ #
2201
+ # lines = ["foo\n", "bar\n", "\n", "baz\n", "qux\n"]
2202
+ # p lines.slice_when {|l1, l2| /\A\s*\z/ =~ l1 && /\S/ =~ l2 }.to_a
2203
+ # #=> [["foo\n", "bar\n", "\n"], ["baz\n", "qux\n"]]
2204
+ #
2205
+ # Enumerable#chunk_while does the same, except splitting when the block returns
2206
+ # `false` instead of `true`.
2154
2207
  #
2155
2208
  def slice_when: () { (Elem elt_before, Elem elt_after) -> boolish } -> ::Enumerator[::Array[Elem], void]
2156
2209
 
2157
2210
  # <!--
2158
2211
  # rdoc-file=enum.c
2159
- # - slice_after(pattern) -> enumerator
2160
- # - slice_after {|array| ... } -> enumerator
2212
+ # - enum.slice_after(pattern) -> an_enumerator
2213
+ # - enum.slice_after { |elt| bool } -> an_enumerator
2161
2214
  # -->
2162
- # With argument `pattern`, returns an enumerator that uses the pattern to
2163
- # partition elements into arrays ("slices"). An element ends the current slice
2164
- # if `element === pattern`:
2165
- #
2166
- # a = %w[foo bar fop for baz fob fog bam foy]
2167
- # e = a.slice_after(/ba/) # => #<Enumerator: ...>
2168
- # e.each {|array| p array }
2215
+ # Creates an enumerator for each chunked elements. The ends of chunks are
2216
+ # defined by *pattern* and the block.
2169
2217
  #
2170
- # Output:
2171
- #
2172
- # ["foo", "bar"]
2173
- # ["fop", "for", "baz"]
2174
- # ["fob", "fog", "bam"]
2175
- # ["foy"]
2176
- #
2177
- # With a block, returns an enumerator that uses the block to partition elements
2178
- # into arrays. An element ends the current slice if its block return is a truthy
2179
- # value:
2218
+ # If *`pattern* === *elt`* returns `true` or the block returns `true` for the
2219
+ # element, the element is end of a chunk.
2180
2220
  #
2181
- # e = (1..20).slice_after {|i| i % 4 == 2 } # => #<Enumerator: ...>
2182
- # e.each {|array| p array }
2221
+ # The `===` and *block* is called from the first element to the last element of
2222
+ # *enum*.
2183
2223
  #
2184
- # Output:
2224
+ # The result enumerator yields the chunked elements as an array. So `each`
2225
+ # method can be called as follows:
2185
2226
  #
2186
- # [1, 2]
2187
- # [3, 4, 5, 6]
2188
- # [7, 8, 9, 10]
2189
- # [11, 12, 13, 14]
2190
- # [15, 16, 17, 18]
2191
- # [19, 20]
2227
+ # enum.slice_after(pattern).each { |ary| ... }
2228
+ # enum.slice_after { |elt| bool }.each { |ary| ... }
2192
2229
  #
2193
2230
  # Other methods of the Enumerator class and Enumerable module, such as `map`,
2194
2231
  # etc., are also usable.
@@ -2209,7 +2246,7 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
2209
2246
  # <!--
2210
2247
  # rdoc-file=enum.c
2211
2248
  # - slice_before(pattern) -> enumerator
2212
- # - slice_before {|array| ... } -> enumerator
2249
+ # - slice_before {|elt| ... } -> enumerator
2213
2250
  # -->
2214
2251
  # With argument `pattern`, returns an enumerator that uses the pattern to
2215
2252
  # partition elements into arrays ("slices"). An element begins a new slice if
@@ -0,0 +1,96 @@
1
+ %a{annotate:rdoc:skip}
2
+ class Enumerator[unchecked out Elem, out Return]
3
+ # <!-- rdoc-file=enumerator.c -->
4
+ # Enumerator::Product generates a Cartesian product of any number of enumerable
5
+ # objects. Iterating over the product of enumerable objects is roughly
6
+ # equivalent to nested each_entry loops where the loop for the rightmost object
7
+ # is put innermost.
8
+ #
9
+ # innings = Enumerator::Product.new(1..9, ['top', 'bottom'])
10
+ #
11
+ # innings.each do |i, h|
12
+ # p [i, h]
13
+ # end
14
+ # # [1, "top"]
15
+ # # [1, "bottom"]
16
+ # # [2, "top"]
17
+ # # [2, "bottom"]
18
+ # # [3, "top"]
19
+ # # [3, "bottom"]
20
+ # # ...
21
+ # # [9, "top"]
22
+ # # [9, "bottom"]
23
+ #
24
+ # The method used against each enumerable object is `each_entry` instead of
25
+ # `each` so that the product of N enumerable objects yields an array of exactly
26
+ # N elements in each iteration.
27
+ #
28
+ # When no enumerator is given, it calls a given block once yielding an empty
29
+ # argument list.
30
+ #
31
+ # This type of objects can be created by Enumerator.product.
32
+ #
33
+ class Product[unchecked out Elem] < Enumerator[Elem, Product[Elem]]
34
+ # <!--
35
+ # rdoc-file=enumerator.c
36
+ # - Enumerator::Product.new(*enums) -> enum
37
+ # -->
38
+ # Generates a new enumerator object that generates a Cartesian product of given
39
+ # enumerable objects.
40
+ #
41
+ # e = Enumerator::Product.new(1..3, [4, 5])
42
+ # e.to_a #=> [[1, 4], [1, 5], [2, 4], [2, 5], [3, 4], [3, 5]]
43
+ # e.size #=> 6
44
+ #
45
+ def initialize: (*_EachEntry[Elem]) -> void
46
+
47
+ include Enumerable[Elem]
48
+
49
+ public
50
+
51
+ # <!--
52
+ # rdoc-file=enumerator.c
53
+ # - obj.each { |...| ... } -> obj
54
+ # - obj.each -> enumerator
55
+ # -->
56
+ # Iterates over the elements of the first enumerable by calling the "each_entry"
57
+ # method on it with the given arguments, then proceeds to the following
58
+ # enumerables in sequence until all of the enumerables are exhausted.
59
+ #
60
+ # If no block is given, returns an enumerator. Otherwise, returns self.
61
+ #
62
+ def each: () { (Array[Elem]) -> void } -> self
63
+
64
+ # <!--
65
+ # rdoc-file=enumerator.c
66
+ # - obj.inspect -> string
67
+ # -->
68
+ # Returns a printable version of the product enumerator.
69
+ #
70
+ def inspect: () -> String
71
+
72
+ # <!--
73
+ # rdoc-file=enumerator.c
74
+ # - obj.rewind -> obj
75
+ # -->
76
+ # Rewinds the product enumerator by calling the "rewind" method on each
77
+ # enumerable in reverse order. Each call is performed only if the enumerable
78
+ # responds to the method.
79
+ #
80
+ def rewind: () -> self
81
+
82
+ # <!--
83
+ # rdoc-file=enumerator.c
84
+ # - obj.size -> int, Float::INFINITY or nil
85
+ # -->
86
+ # Returns the total size of the enumerator product calculated by multiplying the
87
+ # sizes of enumerables in the product. If any of the enumerables reports its
88
+ # size as nil or Float::INFINITY, that value is returned as the size.
89
+ #
90
+ def size: () -> (Integer | Float | nil)
91
+
92
+ private
93
+
94
+ def initialize_copy: (Product[Elem]) -> void
95
+ end
96
+ end