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/enumerable.rbs CHANGED
@@ -1,426 +1,2365 @@
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]: _Each[Elem]
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
1
+ # <!-- rdoc-file=enum.c -->
2
+ # ## What's Here
3
+ #
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)
11
+ #
12
+ #
13
+ # ### Methods for Querying
14
+ #
15
+ # These methods return information about the Enumerable other than the elements
16
+ # themselves:
17
+ #
18
+ # #include?, #member?
19
+ # : Returns `true` if self == object, `false` otherwise.
20
+ # #all?
21
+ # : Returns `true` if all elements meet a specified criterion; `false`
22
+ # otherwise.
23
+ # #any?
24
+ # : Returns `true` if any element meets a specified criterion; `false`
25
+ # otherwise.
26
+ # #none?
27
+ # : Returns `true` if no element meets a specified criterion; `false`
28
+ # otherwise.
29
+ # #one?
30
+ # : Returns `true` if exactly one element meets a specified criterion; `false`
31
+ # 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.
37
+ #
38
+ #
39
+ # ### Methods for Fetching
40
+ #
41
+ # These methods return entries from the Enumerable, without modifying it:
42
+ #
43
+ # *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.
56
+ #
57
+ #
58
+ # *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
64
+ # 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.
74
+ #
75
+ #
76
+ # *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
81
+ # 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
+ #
96
+ #
97
+ # ### Methods for Searching and Filtering
98
+ #
99
+ # These methods return elements that meet a specified criterion.
100
+ #
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.
111
+ #
112
+ #
113
+ # ### Methods for Sorting
114
+ #
115
+ # These methods return elements in sorted order.
116
+ #
117
+ # #sort
118
+ # : Returns the elements, sorted by `<=>` or the given block.
119
+ # #sort_by
120
+ # : Returns the elements, sorted by the given block.
121
+ #
122
+ #
123
+ # ### Methods for Iterating
124
+ #
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.
139
+ #
140
+ #
141
+ # ### Other Methods
142
+ #
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
154
+ # 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.
164
+ #
165
+ #
166
+ # ## Usage
167
+ #
168
+ # To use module Enumerable in a collection class:
169
+ #
170
+ # * Include it:
171
+ #
172
+ # include Enumerable
173
+ #
174
+ # * Implement method `#each` which must yield successive elements of the
175
+ # collection. The method will be called by almost any Enumerable method.
176
+ #
177
+ #
178
+ # Example:
179
+ #
180
+ # class Foo
181
+ # include Enumerable
182
+ # def each
183
+ # yield 1
184
+ # yield 1, 2
185
+ # yield
186
+ # end
187
+ # end
188
+ # Foo.new.each_entry{ |element| p element }
189
+ #
190
+ # Output:
191
+ #
192
+ # 1
193
+ # [1, 2]
194
+ # nil
195
+ #
196
+ # ## Enumerable in Ruby Core Classes
197
+ # Some Ruby classes include Enumerable:
198
+ # * Array
199
+ # * Dir
200
+ # * Hash
201
+ # * IO
202
+ # * Range
203
+ # * Set
204
+ # * Struct
205
+ #
206
+ # Virtually all methods in Enumerable call method `#each` in the including
207
+ # class:
208
+ # * `Hash#each` yields the next key-value pair as a 2-element Array.
209
+ # * `Struct#each` yields the next name-value pair as a 2-element Array.
210
+ # * For the other classes above, `#each` yields the next object from the
211
+ # collection.
212
+ #
213
+ #
214
+ # ## About the Examples
215
+ # The example code snippets for the Enumerable methods:
216
+ # * Always show the use of one or more Array-like classes (often Array
217
+ # itself).
218
+ # * Sometimes show the use of a Hash-like class. For some methods, though, the
219
+ # usage would not make sense, and so it is not shown. Example: #tally would
220
+ # find exactly one of each Hash entry.
221
+ #
222
+ module Enumerable[unchecked out Elem] : _Each[Elem]
223
+ # <!--
224
+ # rdoc-file=enum.c
225
+ # - all? -> true or false
226
+ # - all?(pattern) -> true or false
227
+ # - all? {|element| ... } -> true or false
228
+ # -->
229
+ # Returns whether every element meets a given criterion.
230
+ #
231
+ # With no argument and no block, returns whether every element is truthy:
232
+ #
233
+ # (1..4).all? # => true
234
+ # %w[a b c d].all? # => true
235
+ # [1, 2, nil].all? # => false
236
+ # ['a','b', false].all? # => false
237
+ # [].all? # => true
238
+ #
239
+ # With argument `pattern` and no block, returns whether for each element
240
+ # `element`, `pattern === element`:
241
+ #
242
+ # (1..4).all?(Integer) # => true
243
+ # (1..4).all?(Numeric) # => true
244
+ # (1..4).all?(Float) # => false
245
+ # %w[bar baz bat bam].all?(/ba/) # => true
246
+ # %w[bar baz bat bam].all?(/bar/) # => false
247
+ # %w[bar baz bat bam].all?('ba') # => false
248
+ # {foo: 0, bar: 1, baz: 2}.all?(Array) # => true
249
+ # {foo: 0, bar: 1, baz: 2}.all?(Hash) # => false
250
+ # [].all?(Integer) # => true
251
+ #
252
+ # With a block given, returns whether the block returns a truthy value for every
253
+ # element:
254
+ #
255
+ # (1..4).all? {|element| element < 5 } # => true
256
+ # (1..4).all? {|element| element < 4 } # => false
257
+ # {foo: 0, bar: 1, baz: 2}.all? {|key, value| value < 3 } # => true
258
+ # {foo: 0, bar: 1, baz: 2}.all? {|key, value| value < 2 } # => false
259
+ #
260
+ # Related: #any?, #none? #one?.
261
+ #
24
262
  def all?: () -> bool
25
263
  | () { (Elem) -> boolish } -> bool
26
264
 
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) -> boolish } -> bool
265
+ # <!--
266
+ # rdoc-file=enum.c
267
+ # - any? -> true or false
268
+ # - any?(pattern) -> true or false
269
+ # - any? {|element| ... } -> true or false
270
+ # -->
271
+ # Returns whether any element meets a given criterion.
272
+ #
273
+ # With no argument and no block, returns whether any element is truthy:
274
+ #
275
+ # (1..4).any? # => true
276
+ # %w[a b c d].any? # => true
277
+ # [1, false, nil].any? # => true
278
+ # [].any? # => false
279
+ #
280
+ # With argument `pattern` and no block, returns whether for any element
281
+ # `element`, `pattern === element`:
282
+ #
283
+ # [nil, false, 0].any?(Integer) # => true
284
+ # [nil, false, 0].any?(Numeric) # => true
285
+ # [nil, false, 0].any?(Float) # => false
286
+ # %w[bar baz bat bam].any?(/m/) # => true
287
+ # %w[bar baz bat bam].any?(/foo/) # => false
288
+ # %w[bar baz bat bam].any?('ba') # => false
289
+ # {foo: 0, bar: 1, baz: 2}.any?(Array) # => true
290
+ # {foo: 0, bar: 1, baz: 2}.any?(Hash) # => false
291
+ # [].any?(Integer) # => false
292
+ #
293
+ # With a block given, returns whether the block returns a truthy value for any
294
+ # element:
295
+ #
296
+ # (1..4).any? {|element| element < 2 } # => true
297
+ # (1..4).any? {|element| element < 1 } # => false
298
+ # {foo: 0, bar: 1, baz: 2}.any? {|key, value| value < 1 } # => true
299
+ # {foo: 0, bar: 1, baz: 2}.any? {|key, value| value < 0 } # => false
300
+ #
301
+ # Related: #all?, #none?, #one?.
302
+ #
303
+ def any?: () -> bool
304
+ | () { (Elem) -> boolish } -> bool
47
305
 
306
+ # <!--
307
+ # rdoc-file=enum.c
308
+ # - map {|element| ... } -> array
309
+ # - map -> enumerator
310
+ # -->
311
+ # Returns an array of objects returned by the block.
312
+ #
313
+ # With a block given, calls the block with successive elements; returns an array
314
+ # of the objects returned by the block:
315
+ #
316
+ # (0..4).map {|i| i*i } # => [0, 1, 4, 9, 16]
317
+ # {foo: 0, bar: 1, baz: 2}.map {|key, value| value*2} # => [0, 2, 4]
318
+ #
319
+ # With no block given, returns an Enumerator.
320
+ #
48
321
  def collect: [U] () { (Elem arg0) -> U } -> ::Array[U]
49
322
  | () -> ::Enumerator[Elem, ::Array[untyped]]
50
323
 
324
+ # <!-- rdoc-file=enum.c -->
325
+ # Returns an array of flattened objects returned by the block.
326
+ #
327
+ # With a block given, calls the block with successive elements; returns a
328
+ # flattened array of objects returned by the block:
329
+ #
330
+ # [0, 1, 2, 3].flat_map {|element| -element } # => [0, -1, -2, -3]
331
+ # [0, 1, 2, 3].flat_map {|element| [element, -element] } # => [0, 0, 1, -1, 2, -2, 3, -3]
332
+ # [[0, 1], [2, 3]].flat_map {|e| e + [100] } # => [0, 1, 100, 2, 3, 100]
333
+ # {foo: 0, bar: 1, baz: 2}.flat_map {|key, value| [key, value] } # => [:foo, 0, :bar, 1, :baz, 2]
334
+ #
335
+ # With no block given, returns an Enumerator.
336
+ #
337
+ # Alias: #collect_concat.
338
+ #
51
339
  def collect_concat: [U] () { (Elem) -> (::Array[U] | U) } -> ::Array[U]
52
340
  | () -> ::Enumerator[Elem, ::Array[untyped]]
53
341
 
54
- # Returns the number of items in `enum` through enumeration. If an
55
- # argument is given, the number of items in `enum` that are equal to
56
- # `item` are counted. If a block is given, it counts the number of
57
- # elements yielding a true value.
58
- #
59
- # ```ruby
60
- # ary = [1, 2, 4, 2]
61
- # ary.count #=> 4
62
- # ary.count(2) #=> 2
63
- # ary.count{ |x| x%2==0 } #=> 3
64
- # ```
342
+ # <!--
343
+ # rdoc-file=enum.c
344
+ # - compact -> array
345
+ # -->
346
+ # Returns an array of all non-`nil` elements:
347
+ #
348
+ # a = [nil, 0, nil, 'a', false, nil, false, nil, 'a', nil, 0, nil]
349
+ # a.compact # => [0, "a", false, false, "a", 0]
350
+ #
351
+ def compact: () -> Array[Elem]
352
+
353
+ # <!--
354
+ # rdoc-file=enum.c
355
+ # - count -> integer
356
+ # - count(object) -> integer
357
+ # - count {|element| ... } -> integer
358
+ # -->
359
+ # Returns the count of elements, based on an argument or block criterion, if
360
+ # given.
361
+ #
362
+ # With no argument and no block given, returns the number of elements:
363
+ #
364
+ # [0, 1, 2].count # => 3
365
+ # {foo: 0, bar: 1, baz: 2}.count # => 3
366
+ #
367
+ # With argument `object` given, returns the number of elements that are `==` to
368
+ # `object`:
369
+ #
370
+ # [0, 1, 2, 1].count(1) # => 2
371
+ #
372
+ # With a block given, calls the block with each element and returns the number
373
+ # of elements for which the block returns a truthy value:
374
+ #
375
+ # [0, 1, 2, 3].count {|element| element < 2} # => 2
376
+ # {foo: 0, bar: 1, baz: 2}.count {|key, value| value < 2} # => 2
377
+ #
65
378
  def count: () -> Integer
66
379
  | (Elem) -> Integer
67
380
  | () { (Elem) -> boolish } -> Integer
68
381
 
382
+ # <!--
383
+ # rdoc-file=enum.c
384
+ # - cycle(n = nil) {|element| ...} -> nil
385
+ # - cycle(n = nil) -> enumerator
386
+ # -->
387
+ # When called with positive integer argument `n` and a block, calls the block
388
+ # with each element, then does so again, until it has done so `n` times; returns
389
+ # `nil`:
390
+ #
391
+ # a = []
392
+ # (1..4).cycle(3) {|element| a.push(element) } # => nil
393
+ # a # => [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]
394
+ # a = []
395
+ # ('a'..'d').cycle(2) {|element| a.push(element) }
396
+ # a # => ["a", "b", "c", "d", "a", "b", "c", "d"]
397
+ # a = []
398
+ # {foo: 0, bar: 1, baz: 2}.cycle(2) {|element| a.push(element) }
399
+ # a # => [[:foo, 0], [:bar, 1], [:baz, 2], [:foo, 0], [:bar, 1], [:baz, 2]]
400
+ #
401
+ # If count is zero or negative, does not call the block.
402
+ #
403
+ # When called with a block and `n` is `nil`, cycles forever.
404
+ #
405
+ # When no block is given, returns an Enumerator.
406
+ #
69
407
  def cycle: (?Integer n) { (Elem arg0) -> untyped } -> NilClass
70
408
  | (?Integer n) -> ::Enumerator[Elem, NilClass]
71
409
 
410
+ # <!-- rdoc-file=enum.c -->
411
+ # Returns the first element for which the block returns a truthy value.
412
+ #
413
+ # With a block given, calls the block with successive elements of the
414
+ # collection; returns the first element for which the block returns a truthy
415
+ # value:
416
+ #
417
+ # (0..9).find {|element| element > 2} # => 3
418
+ #
419
+ # If no such element is found, calls `if_none_proc` and returns its return
420
+ # value.
421
+ #
422
+ # (0..9).find(proc {false}) {|element| element > 12} # => false
423
+ # {foo: 0, bar: 1, baz: 2}.find {|key, value| key.start_with?('b') } # => [:bar, 1]
424
+ # {foo: 0, bar: 1, baz: 2}.find(proc {[]}) {|key, value| key.start_with?('c') } # => []
425
+ #
426
+ # With no block given, returns an Enumerator.
427
+ #
72
428
  def detect: (?Proc ifnone) { (Elem) -> boolish } -> Elem?
73
429
  | (?Proc ifnone) -> ::Enumerator[Elem, Elem?]
74
430
 
431
+ # <!--
432
+ # rdoc-file=enum.c
433
+ # - drop(n) -> array
434
+ # -->
435
+ # For positive integer `n`, returns an array containing all but the first `n`
436
+ # elements:
437
+ #
438
+ # r = (1..4)
439
+ # r.drop(3) # => [4]
440
+ # r.drop(2) # => [3, 4]
441
+ # r.drop(1) # => [2, 3, 4]
442
+ # r.drop(0) # => [1, 2, 3, 4]
443
+ # r.drop(50) # => []
444
+ #
445
+ # h = {foo: 0, bar: 1, baz: 2, bat: 3}
446
+ # h.drop(2) # => [[:baz, 2], [:bat, 3]]
447
+ #
75
448
  def drop: (Integer n) -> ::Array[Elem]
76
449
 
450
+ # <!--
451
+ # rdoc-file=enum.c
452
+ # - drop_while {|element| ... } -> array
453
+ # - drop_while -> enumerator
454
+ # -->
455
+ # Calls the block with successive elements as long as the block returns a truthy
456
+ # value; returns an array of all elements after that point:
457
+ #
458
+ # (1..4).drop_while{|i| i < 3 } # => [3, 4]
459
+ # h = {foo: 0, bar: 1, baz: 2}
460
+ # a = h.drop_while{|element| key, value = *element; value < 2 }
461
+ # a # => [[:baz, 2]]
462
+ #
463
+ # With no block given, returns an Enumerator.
464
+ #
77
465
  def drop_while: () { (Elem) -> boolish } -> ::Array[Elem]
78
466
  | () -> ::Enumerator[Elem, ::Array[Elem]]
79
467
 
80
- def each_cons: (Integer n) { (::Array[Elem] arg0) -> untyped } -> NilClass
81
- | (Integer n) -> ::Enumerator[::Array[Elem], NilClass]
468
+ # <!--
469
+ # rdoc-file=enum.c
470
+ # - each_cons(n) { ... } -> self
471
+ # - each_cons(n) -> enumerator
472
+ # -->
473
+ # Calls the block with each successive overlapped `n`-tuple of elements; returns
474
+ # `self`:
475
+ #
476
+ # a = []
477
+ # (1..5).each_cons(3) {|element| a.push(element) }
478
+ # a # => [[1, 2, 3], [2, 3, 4], [3, 4, 5]]
479
+ #
480
+ # a = []
481
+ # h = {foo: 0, bar: 1, baz: 2, bam: 3}
482
+ # h.each_cons(2) {|element| a.push(element) }
483
+ # a # => [[[:foo, 0], [:bar, 1]], [[:bar, 1], [:baz, 2]], [[:baz, 2], [:bam, 3]]]
484
+ #
485
+ # With no block given, returns an Enumerator.
486
+ #
487
+ def each_cons: (Integer n) { (::Array[Elem]) -> void } -> self
488
+ | (Integer n) -> ::Enumerator[::Array[Elem], self]
82
489
 
490
+ # <!--
491
+ # rdoc-file=enum.c
492
+ # - each_with_index(*args) {|element, i| ..... } -> self
493
+ # - each_with_index(*args) -> enumerator
494
+ # -->
495
+ # With a block given, calls the block with each element and its index; returns
496
+ # `self`:
497
+ #
498
+ # h = {}
499
+ # (1..4).each_with_index {|element, i| h[element] = i } # => 1..4
500
+ # h # => {1=>0, 2=>1, 3=>2, 4=>3}
501
+ #
502
+ # h = {}
503
+ # %w[a b c d].each_with_index {|element, i| h[element] = i }
504
+ # # => ["a", "b", "c", "d"]
505
+ # h # => {"a"=>0, "b"=>1, "c"=>2, "d"=>3}
506
+ #
507
+ # a = []
508
+ # h = {foo: 0, bar: 1, baz: 2}
509
+ # h.each_with_index {|element, i| a.push([i, element]) }
510
+ # # => {:foo=>0, :bar=>1, :baz=>2}
511
+ # a # => [[0, [:foo, 0]], [1, [:bar, 1]], [2, [:baz, 2]]]
512
+ #
513
+ # With no block given, returns an Enumerator.
514
+ #
83
515
  def each_with_index: () { (Elem, Integer index) -> untyped } -> self
84
516
  | () -> ::Enumerator[[ Elem, Integer ], self]
85
517
 
518
+ # <!--
519
+ # rdoc-file=enum.c
520
+ # - each_with_object(object) { |(*args), memo_object| ... } -> object
521
+ # - each_with_object(object) -> enumerator
522
+ # -->
523
+ # Calls the block once for each element, passing both the element and the given
524
+ # object:
525
+ #
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 }
528
+ # # => {0=>:foo, 1=>:bar, 2=>:baz}
529
+ #
530
+ # With no block given, returns an Enumerator.
531
+ #
86
532
  def each_with_object: [U] (U obj) { (Elem, U obj) -> untyped } -> U
87
533
  | [U] (U obj) -> ::Enumerator[[ Elem, U ], U]
88
534
 
89
- # Returns an array containing the items in *enum* .
535
+ # <!-- rdoc-file=enum.c -->
536
+ # Returns an array containing the items in `self`:
537
+ #
538
+ # (0..4).to_a # => [0, 1, 2, 3, 4]
90
539
  #
91
- # ```ruby
92
- # (1..7).to_a #=> [1, 2, 3, 4, 5, 6, 7]
93
- # { 'a'=>1, 'b'=>2, 'c'=>3 }.to_a #=> [["a", 1], ["b", 2], ["c", 3]]
540
+ # Enumerable#entries is an alias for Enumerable#to_a.
94
541
  #
95
- # require 'prime'
96
- # Prime.entries 10 #=> [2, 3, 5, 7]
97
- # ```
98
542
  def entries: () -> ::Array[Elem]
99
543
 
544
+ # <!--
545
+ # rdoc-file=enum.c
546
+ # - select {|element| ... } -> array
547
+ # - select -> enumerator
548
+ # -->
549
+ # Returns an array containing elements selected by the block.
550
+ #
551
+ # With a block given, calls the block with successive elements; returns an array
552
+ # of those elements for which the block returns a truthy value:
553
+ #
554
+ # (0..9).select {|element| element % 3 == 0 } # => [0, 3, 6, 9]
555
+ # a = {foo: 0, bar: 1, baz: 2}.select {|key, value| key.start_with?('b') }
556
+ # a # => {:bar=>1, :baz=>2}
557
+ #
558
+ # With no block given, returns an Enumerator.
559
+ #
560
+ # Related: #reject.
561
+ #
100
562
  def find_all: () { (Elem) -> boolish } -> ::Array[Elem]
101
563
  | () -> ::Enumerator[Elem, ::Array[Elem]]
102
564
 
565
+ # <!-- rdoc-file=enum.c -->
566
+ # Returns an array containing elements selected by the block.
567
+ #
568
+ # With a block given, calls the block with successive elements; returns an array
569
+ # of those elements for which the block returns a truthy value:
570
+ #
571
+ # (0..9).select {|element| element % 3 == 0 } # => [0, 3, 6, 9]
572
+ # a = {foo: 0, bar: 1, baz: 2}.select {|key, value| key.start_with?('b') }
573
+ # a # => {:bar=>1, :baz=>2}
574
+ #
575
+ # With no block given, returns an Enumerator.
576
+ #
577
+ # Related: #reject.
578
+ #
103
579
  alias select find_all
580
+
581
+ # <!-- rdoc-file=enum.c -->
582
+ # Returns an array containing elements selected by the block.
583
+ #
584
+ # With a block given, calls the block with successive elements; returns an array
585
+ # of those elements for which the block returns a truthy value:
586
+ #
587
+ # (0..9).select {|element| element % 3 == 0 } # => [0, 3, 6, 9]
588
+ # a = {foo: 0, bar: 1, baz: 2}.select {|key, value| key.start_with?('b') }
589
+ # a # => {:bar=>1, :baz=>2}
590
+ #
591
+ # With no block given, returns an Enumerator.
592
+ #
593
+ # Related: #reject.
594
+ #
104
595
  alias filter find_all
105
596
 
597
+ # <!--
598
+ # rdoc-file=enum.c
599
+ # - find_index(object) -> integer or nil
600
+ # - find_index {|element| ... } -> integer or nil
601
+ # - find_index -> enumerator
602
+ # -->
603
+ # Returns the index of the first element that meets a specified criterion, or
604
+ # `nil` if no such element is found.
605
+ #
606
+ # With argument `object` given, returns the index of the first element that is
607
+ # `==` `object`:
608
+ #
609
+ # ['a', 'b', 'c', 'b'].find_index('b') # => 1
610
+ #
611
+ # With a block given, calls the block with successive elements; returns the
612
+ # first element for which the block returns a truthy value:
613
+ #
614
+ # ['a', 'b', 'c', 'b'].find_index {|element| element.start_with?('b') } # => 1
615
+ # {foo: 0, bar: 1, baz: 2}.find_index {|key, value| value > 1 } # => 2
616
+ #
617
+ # With no argument and no block given, returns an Enumerator.
618
+ #
106
619
  def find_index: (untyped value) -> Integer?
107
620
  | () { (Elem) -> boolish } -> Integer?
108
621
  | () -> ::Enumerator[Elem, Integer?]
109
622
 
110
- # Returns the first element, or the first `n` elements, of the enumerable.
111
- # If the enumerable is empty, the first form returns `nil`, and the
112
- # second form returns an empty array.
113
- #
114
- # ```ruby
115
- # %w[foo bar baz].first #=> "foo"
116
- # %w[foo bar baz].first(2) #=> ["foo", "bar"]
117
- # %w[foo bar baz].first(10) #=> ["foo", "bar", "baz"]
118
- # [].first #=> nil
119
- # [].first(10) #=> []
120
- # ```
623
+ # <!--
624
+ # rdoc-file=enum.c
625
+ # - first -> element or nil
626
+ # - first(n) -> array
627
+ # -->
628
+ # Returns the first element or elements.
629
+ #
630
+ # With no argument, returns the first element, or `nil` if there is none:
631
+ #
632
+ # (1..4).first # => 1
633
+ # %w[a b c].first # => "a"
634
+ # {foo: 1, bar: 1, baz: 2}.first # => [:foo, 1]
635
+ # [].first # => nil
636
+ #
637
+ # With integer argument `n`, returns an array containing the first `n` elements
638
+ # that exist:
639
+ #
640
+ # (1..4).first(2) # => [1, 2]
641
+ # %w[a b c d].first(3) # => ["a", "b", "c"]
642
+ # %w[a b c d].first(50) # => ["a", "b", "c", "d"]
643
+ # {foo: 1, bar: 1, baz: 2}.first(2) # => [[:foo, 1], [:bar, 1]]
644
+ # [].first(2) # => []
645
+ #
121
646
  def first: () -> Elem?
122
647
  | (_ToInt n) -> ::Array[Elem]
123
648
 
649
+ # <!--
650
+ # rdoc-file=enum.c
651
+ # - grep(pattern) -> array
652
+ # - grep(pattern) {|element| ... } -> array
653
+ # -->
654
+ # Returns an array of objects based elements of `self` that match the given
655
+ # pattern.
656
+ #
657
+ # With no block given, returns an array containing each element for which
658
+ # `pattern === element` is `true`:
659
+ #
660
+ # a = ['foo', 'bar', 'car', 'moo']
661
+ # a.grep(/ar/) # => ["bar", "car"]
662
+ # (1..10).grep(3..8) # => [3, 4, 5, 6, 7, 8]
663
+ # ['a', 'b', 0, 1].grep(Integer) # => [0, 1]
664
+ #
665
+ # With a block given, calls the block with each matching element and returns an
666
+ # array containing each object returned by the block:
667
+ #
668
+ # a = ['foo', 'bar', 'car', 'moo']
669
+ # a.grep(/ar/) {|element| element.upcase } # => ["BAR", "CAR"]
670
+ #
671
+ # Related: #grep_v.
672
+ #
124
673
  def grep: (untyped arg0) -> ::Array[Elem]
125
674
  | [U] (untyped arg0) { (Elem arg0) -> U } -> ::Array[U]
126
675
 
676
+ # <!--
677
+ # rdoc-file=enum.c
678
+ # - grep_v(pattern) -> array
679
+ # - grep_v(pattern) {|element| ... } -> array
680
+ # -->
681
+ # Returns an array of objects based on elements of `self` that *don't* match the
682
+ # given pattern.
683
+ #
684
+ # With no block given, returns an array containing each element for which
685
+ # `pattern === element` is `false`:
686
+ #
687
+ # a = ['foo', 'bar', 'car', 'moo']
688
+ # a.grep_v(/ar/) # => ["foo", "moo"]
689
+ # (1..10).grep_v(3..8) # => [1, 2, 9, 10]
690
+ # ['a', 'b', 0, 1].grep_v(Integer) # => ["a", "b"]
691
+ #
692
+ # With a block given, calls the block with each non-matching element and returns
693
+ # an array containing each object returned by the block:
694
+ #
695
+ # a = ['foo', 'bar', 'car', 'moo']
696
+ # a.grep_v(/ar/) {|element| element.upcase } # => ["FOO", "MOO"]
697
+ #
698
+ # Related: #grep.
699
+ #
127
700
  def grep_v: (untyped) -> ::Array[Elem]
128
701
  | [U] (untyped) { (Elem) -> U } -> ::Array[U]
129
702
 
703
+ # <!--
704
+ # rdoc-file=enum.c
705
+ # - group_by {|element| ... } -> hash
706
+ # - group_by -> enumerator
707
+ # -->
708
+ # With a block given returns a hash:
709
+ #
710
+ # * Each key is a return value from the block.
711
+ # * Each value is an array of those elements for which the block returned that
712
+ # key.
713
+ #
714
+ #
715
+ # Examples:
716
+ #
717
+ # g = (1..6).group_by {|i| i%3 }
718
+ # g # => {1=>[1, 4], 2=>[2, 5], 0=>[3, 6]}
719
+ # h = {foo: 0, bar: 1, baz: 0, bat: 1}
720
+ # g = h.group_by {|key, value| value }
721
+ # g # => {0=>[[:foo, 0], [:baz, 0]], 1=>[[:bar, 1], [:bat, 1]]}
722
+ #
723
+ # With no block given, returns an Enumerator.
724
+ #
130
725
  def group_by: [U] () { (Elem arg0) -> U } -> ::Hash[U, ::Array[Elem]]
131
726
  | () -> ::Enumerator[Elem, ::Array[Elem]]
132
727
 
133
- def `include?`: (Elem arg0) -> bool
728
+ # <!-- rdoc-file=enum.c -->
729
+ # Returns whether for any element `object == element`:
730
+ #
731
+ # (1..4).include?(2) # => true
732
+ # (1..4).include?(5) # => false
733
+ # (1..4).include?('2') # => false
734
+ # %w[a b c d].include?('b') # => true
735
+ # %w[a b c d].include?('2') # => false
736
+ # {foo: 0, bar: 1, baz: 2}.include?(:foo) # => true
737
+ # {foo: 0, bar: 1, baz: 2}.include?('foo') # => false
738
+ # {foo: 0, bar: 1, baz: 2}.include?(0) # => false
739
+ #
740
+ # Enumerable#member? is an alias for Enumerable#include?.
741
+ #
742
+ def include?: (Elem arg0) -> bool
134
743
 
744
+ # <!--
745
+ # rdoc-file=enum.c
746
+ # - inject(symbol) -> object
747
+ # - inject(initial_operand, symbol) -> object
748
+ # - inject {|memo, operand| ... } -> object
749
+ # - inject(initial_operand) {|memo, operand| ... } -> object
750
+ # -->
751
+ # Returns an object formed from operands via either:
752
+ #
753
+ # * A method named by `symbol`.
754
+ # * A block to which each operand is passed.
755
+ #
756
+ #
757
+ # With method-name argument `symbol`, combines operands using the method:
758
+ #
759
+ # # Sum, without initial_operand.
760
+ # (1..4).inject(:+) # => 10
761
+ # # Sum, with initial_operand.
762
+ # (1..4).inject(10, :+) # => 20
763
+ #
764
+ # With a block, passes each operand to the block:
765
+ #
766
+ # # Sum of squares, without initial_operand.
767
+ # (1..4).inject {|sum, n| sum + n*n } # => 30
768
+ # # Sum of squares, with initial_operand.
769
+ # (1..4).inject(2) {|sum, n| sum + n*n } # => 32
770
+ #
771
+ # **Operands**
772
+ #
773
+ # If argument `initial_operand` is not given, the operands for `inject` are
774
+ # simply the elements of `self`. Example calls and their operands:
775
+ #
776
+ # `(1..4).inject(:+)`
777
+ # : `[1, 2, 3, 4]`.
778
+ #
779
+ # `(1...4).inject(:+)`
780
+ # : `[1, 2, 3]`.
781
+ #
782
+ # `('a'..'d').inject(:+)`
783
+ # : `['a', 'b', 'c', 'd']`.
784
+ #
785
+ # `('a'...'d').inject(:+)`
786
+ # : `['a', 'b', 'c']`.
787
+ #
788
+ #
789
+ #
790
+ # Examples with first operand (which is `self.first`) of various types:
791
+ #
792
+ # # Integer.
793
+ # (1..4).inject(:+) # => 10
794
+ # # Float.
795
+ # [1.0, 2, 3, 4].inject(:+) # => 10.0
796
+ # # Character.
797
+ # ('a'..'d').inject(:+) # => "abcd"
798
+ # # Complex.
799
+ # [Complex(1, 2), 3, 4].inject(:+) # => (8+2i)
800
+ #
801
+ # If argument `initial_operand` is given, the operands for `inject` are that
802
+ # value plus the elements of `self`. Example calls their operands:
803
+ #
804
+ # `(1..4).inject(10, :+)`
805
+ # : `[10, 1, 2, 3, 4]`.
806
+ #
807
+ # `(1...4).inject(10, :+)`
808
+ # : `[10, 1, 2, 3]`.
809
+ #
810
+ # `('a'..'d').inject('e', :+)`
811
+ # : `['e', 'a', 'b', 'c', 'd']`.
812
+ #
813
+ # `('a'...'d').inject('e', :+)`
814
+ # : `['e', 'a', 'b', 'c']`.
815
+ #
816
+ #
817
+ #
818
+ # Examples with `initial_operand` of various types:
819
+ #
820
+ # # Integer.
821
+ # (1..4).inject(2, :+) # => 12
822
+ # # Float.
823
+ # (1..4).inject(2.0, :+) # => 12.0
824
+ # # String.
825
+ # ('a'..'d').inject('foo', :+) # => "fooabcd"
826
+ # # Array.
827
+ # %w[a b c].inject(['x'], :push) # => ["x", "a", "b", "c"]
828
+ # # Complex.
829
+ # (1..4).inject(Complex(2, 2), :+) # => (12+2i)
830
+ #
831
+ # **Combination by Given \Method**
832
+ #
833
+ # If the method-name argument `symbol` is given, the operands are combined by
834
+ # that method:
835
+ #
836
+ # * The first and second operands are combined.
837
+ # * That result is combined with the third operand.
838
+ # * That result is combined with the fourth operand.
839
+ # * And so on.
840
+ #
841
+ #
842
+ # The return value from `inject` is the result of the last combination.
843
+ #
844
+ # This call to `inject` computes the sum of the operands:
845
+ #
846
+ # (1..4).inject(:+) # => 10
847
+ #
848
+ # Examples with various methods:
849
+ #
850
+ # # Integer addition.
851
+ # (1..4).inject(:+) # => 10
852
+ # # Integer multiplication.
853
+ # (1..4).inject(:*) # => 24
854
+ # # Character range concatenation.
855
+ # ('a'..'d').inject('', :+) # => "abcd"
856
+ # # String array concatenation.
857
+ # %w[foo bar baz].inject('', :+) # => "foobarbaz"
858
+ # # Hash update.
859
+ # h = [{foo: 0, bar: 1}, {baz: 2}, {bat: 3}].inject(:update)
860
+ # h # => {:foo=>0, :bar=>1, :baz=>2, :bat=>3}
861
+ # # Hash conversion to nested arrays.
862
+ # h = {foo: 0, bar: 1}.inject([], :push)
863
+ # h # => [[:foo, 0], [:bar, 1]]
864
+ #
865
+ # **Combination by Given Block**
866
+ #
867
+ # If a block is given, the operands are passed to the block:
868
+ #
869
+ # * The first call passes the first and second operands.
870
+ # * The second call passes the result of the first call, along with the third
871
+ # operand.
872
+ # * The third call passes the result of the second call, along with the fourth
873
+ # operand.
874
+ # * And so on.
875
+ #
876
+ #
877
+ # The return value from `inject` is the return value from the last block call.
878
+ #
879
+ # This call to `inject` gives a block that writes the memo and element, and also
880
+ # sums the elements:
881
+ #
882
+ # (1..4).inject do |memo, element|
883
+ # p "Memo: #{memo}; element: #{element}"
884
+ # memo + element
885
+ # end # => 10
886
+ #
887
+ # Output:
888
+ #
889
+ # "Memo: 1; element: 2"
890
+ # "Memo: 3; element: 3"
891
+ # "Memo: 6; element: 4"
892
+ #
893
+ # Enumerable#reduce is an alias for Enumerable#inject.
894
+ #
135
895
  def inject: (untyped init, Symbol method) -> untyped
136
896
  | (Symbol method) -> untyped
137
897
  | [A] (A initial) { (A, Elem) -> A } -> A
138
898
  | () { (Elem, Elem) -> Elem } -> Elem
139
899
 
140
- # Returns the object in *enum* with the maximum value. The first form
141
- # assumes all objects implement `Comparable` ; the second uses the block
142
- # to return *a \<=\> b* .
143
- #
144
- # ```ruby
145
- # a = %w(albatross dog horse)
146
- # a.max #=> "horse"
147
- # a.max { |a, b| a.length <=> b.length } #=> "albatross"
148
- # ```
149
- #
150
- # If the `n` argument is given, maximum `n` elements are returned as an
151
- # array, sorted in descending order.
152
- #
153
- # ```ruby
154
- # a = %w[albatross dog horse]
155
- # a.max(2) #=> ["horse", "dog"]
156
- # a.max(2) {|a, b| a.length <=> b.length } #=> ["albatross", "horse"]
157
- # [5, 1, 3, 4, 2].max(3) #=> [5, 4, 3]
158
- # ```
900
+ # <!--
901
+ # rdoc-file=enum.c
902
+ # - max -> element
903
+ # - max(n) -> array
904
+ # - max {|a, b| ... } -> element
905
+ # - max(n) {|a, b| ... } -> array
906
+ # -->
907
+ # Returns the element with the maximum element according to a given criterion.
908
+ # The ordering of equal elements is indeterminate and may be unstable.
909
+ #
910
+ # With no argument and no block, returns the maximum element, using the
911
+ # elements' own method `<=>` for comparison:
912
+ #
913
+ # (1..4).max # => 4
914
+ # (-4..-1).max # => -1
915
+ # %w[d c b a].max # => "d"
916
+ # {foo: 0, bar: 1, baz: 2}.max # => [:foo, 0]
917
+ # [].max # => nil
918
+ #
919
+ # With positive integer argument `n` given, and no block, returns an array
920
+ # containing the first `n` maximum elements that exist:
921
+ #
922
+ # (1..4).max(2) # => [4, 3]
923
+ # (-4..-1).max(2) # => [-1, -2]
924
+ # %w[d c b a].max(2) # => ["d", "c"]
925
+ # {foo: 0, bar: 1, baz: 2}.max(2) # => [[:foo, 0], [:baz, 2]]
926
+ # [].max(2) # => []
927
+ #
928
+ # With a block given, the block determines the maximum elements. The block is
929
+ # called with two elements `a` and `b`, and must return:
930
+ #
931
+ # * A negative integer if `a < b`.
932
+ # * Zero if `a == b`.
933
+ # * A positive integer if `a > b`.
934
+ #
935
+ #
936
+ # With a block given and no argument, returns the maximum element as determined
937
+ # by the block:
938
+ #
939
+ # %w[xxx x xxxx xx].max {|a, b| a.size <=> b.size } # => "xxxx"
940
+ # h = {foo: 0, bar: 1, baz: 2}
941
+ # h.max {|pair1, pair2| pair1[1] <=> pair2[1] } # => [:baz, 2]
942
+ # [].max {|a, b| a <=> b } # => nil
943
+ #
944
+ # With a block given and positive integer argument `n` given, returns an array
945
+ # containing the first `n` maximum elements that exist, as determined by the
946
+ # block.
947
+ #
948
+ # %w[xxx x xxxx xx].max(2) {|a, b| a.size <=> b.size } # => ["xxxx", "xxx"]
949
+ # h = {foo: 0, bar: 1, baz: 2}
950
+ # h.max(2) {|pair1, pair2| pair1[1] <=> pair2[1] }
951
+ # # => [[:baz, 2], [:bar, 1]]
952
+ # [].max(2) {|a, b| a <=> b } # => []
953
+ #
954
+ # Related: #min, #minmax, #max_by.
955
+ #
159
956
  def max: () -> Elem?
160
957
  | () { (Elem arg0, Elem arg1) -> Integer } -> Elem?
161
958
  | (Integer arg0) -> ::Array[Elem]
162
959
  | (Integer arg0) { (Elem arg0, Elem arg1) -> Integer } -> ::Array[Elem]
163
960
 
961
+ # <!--
962
+ # rdoc-file=enum.c
963
+ # - max_by {|element| ... } -> element
964
+ # - max_by(n) {|element| ... } -> array
965
+ # - max_by -> enumerator
966
+ # - max_by(n) -> enumerator
967
+ # -->
968
+ # Returns the elements for which the block returns the maximum values.
969
+ #
970
+ # With a block given and no argument, returns the element for which the block
971
+ # returns the maximum value:
972
+ #
973
+ # (1..4).max_by {|element| -element } # => 1
974
+ # %w[a b c d].max_by {|element| -element.ord } # => "a"
975
+ # {foo: 0, bar: 1, baz: 2}.max_by {|key, value| -value } # => [:foo, 0]
976
+ # [].max_by {|element| -element } # => nil
977
+ #
978
+ # With a block given and positive integer argument `n` given, returns an array
979
+ # containing the `n` elements for which the block returns maximum values:
980
+ #
981
+ # (1..4).max_by(2) {|element| -element }
982
+ # # => [1, 2]
983
+ # %w[a b c d].max_by(2) {|element| -element.ord }
984
+ # # => ["a", "b"]
985
+ # {foo: 0, bar: 1, baz: 2}.max_by(2) {|key, value| -value }
986
+ # # => [[:foo, 0], [:bar, 1]]
987
+ # [].max_by(2) {|element| -element }
988
+ # # => []
989
+ #
990
+ # Returns an Enumerator if no block is given.
991
+ #
992
+ # Related: #max, #minmax, #min_by.
993
+ #
164
994
  def max_by: () -> ::Enumerator[Elem, Elem?]
165
995
  | () { (Elem arg0) -> (Comparable | ::Array[untyped]) } -> Elem?
166
996
  | (Integer arg0) -> ::Enumerator[Elem, ::Array[Elem]]
167
997
  | (Integer arg0) { (Elem arg0) -> (Comparable | ::Array[untyped]) } -> ::Array[Elem]
168
998
 
169
- # Returns the object in *enum* with the minimum value. The first form
170
- # assumes all objects implement `Comparable` ; the second uses the block
171
- # to return *a \<=\> b* .
172
- #
173
- # ```ruby
174
- # a = %w(albatross dog horse)
175
- # a.min #=> "albatross"
176
- # a.min { |a, b| a.length <=> b.length } #=> "dog"
177
- # ```
178
- #
179
- # If the `n` argument is given, minimum `n` elements are returned as a
180
- # sorted array.
181
- #
182
- # ```ruby
183
- # a = %w[albatross dog horse]
184
- # a.min(2) #=> ["albatross", "dog"]
185
- # a.min(2) {|a, b| a.length <=> b.length } #=> ["dog", "horse"]
186
- # [5, 1, 3, 4, 2].min(3) #=> [1, 2, 3]
187
- # ```
999
+ # <!--
1000
+ # rdoc-file=enum.c
1001
+ # - min -> element
1002
+ # - min(n) -> array
1003
+ # - min {|a, b| ... } -> element
1004
+ # - min(n) {|a, b| ... } -> array
1005
+ # -->
1006
+ # Returns the element with the minimum element according to a given criterion.
1007
+ # The ordering of equal elements is indeterminate and may be unstable.
1008
+ #
1009
+ # With no argument and no block, returns the minimum element, using the
1010
+ # elements' own method `<=>` for comparison:
1011
+ #
1012
+ # (1..4).min # => 1
1013
+ # (-4..-1).min # => -4
1014
+ # %w[d c b a].min # => "a"
1015
+ # {foo: 0, bar: 1, baz: 2}.min # => [:bar, 1]
1016
+ # [].min # => nil
1017
+ #
1018
+ # With positive integer argument `n` given, and no block, returns an array
1019
+ # containing the first `n` minimum elements that exist:
1020
+ #
1021
+ # (1..4).min(2) # => [1, 2]
1022
+ # (-4..-1).min(2) # => [-4, -3]
1023
+ # %w[d c b a].min(2) # => ["a", "b"]
1024
+ # {foo: 0, bar: 1, baz: 2}.min(2) # => [[:bar, 1], [:baz, 2]]
1025
+ # [].min(2) # => []
1026
+ #
1027
+ # With a block given, the block determines the minimum elements. The block is
1028
+ # called with two elements `a` and `b`, and must return:
1029
+ #
1030
+ # * A negative integer if `a < b`.
1031
+ # * Zero if `a == b`.
1032
+ # * A positive integer if `a > b`.
1033
+ #
1034
+ #
1035
+ # With a block given and no argument, returns the minimum element as determined
1036
+ # by the block:
1037
+ #
1038
+ # %w[xxx x xxxx xx].min {|a, b| a.size <=> b.size } # => "x"
1039
+ # h = {foo: 0, bar: 1, baz: 2}
1040
+ # h.min {|pair1, pair2| pair1[1] <=> pair2[1] } # => [:foo, 0]
1041
+ # [].min {|a, b| a <=> b } # => nil
1042
+ #
1043
+ # With a block given and positive integer argument `n` given, returns an array
1044
+ # containing the first `n` minimum elements that exist, as determined by the
1045
+ # block.
1046
+ #
1047
+ # %w[xxx x xxxx xx].min(2) {|a, b| a.size <=> b.size } # => ["x", "xx"]
1048
+ # h = {foo: 0, bar: 1, baz: 2}
1049
+ # h.min(2) {|pair1, pair2| pair1[1] <=> pair2[1] }
1050
+ # # => [[:foo, 0], [:bar, 1]]
1051
+ # [].min(2) {|a, b| a <=> b } # => []
1052
+ #
1053
+ # Related: #min_by, #minmax, #max.
1054
+ #
188
1055
  def min: () -> Elem?
189
1056
  | () { (Elem arg0, Elem arg1) -> Integer } -> Elem?
190
1057
  | (Integer arg0) -> ::Array[Elem]
191
1058
  | (Integer arg0) { (Elem arg0, Elem arg1) -> Integer } -> ::Array[Elem]
192
1059
 
1060
+ # <!--
1061
+ # rdoc-file=enum.c
1062
+ # - min_by {|element| ... } -> element
1063
+ # - min_by(n) {|element| ... } -> array
1064
+ # - min_by -> enumerator
1065
+ # - min_by(n) -> enumerator
1066
+ # -->
1067
+ # Returns the elements for which the block returns the minimum values.
1068
+ #
1069
+ # With a block given and no argument, returns the element for which the block
1070
+ # returns the minimum value:
1071
+ #
1072
+ # (1..4).min_by {|element| -element } # => 4
1073
+ # %w[a b c d].min_by {|element| -element.ord } # => "d"
1074
+ # {foo: 0, bar: 1, baz: 2}.min_by {|key, value| -value } # => [:baz, 2]
1075
+ # [].min_by {|element| -element } # => nil
1076
+ #
1077
+ # With a block given and positive integer argument `n` given, returns an array
1078
+ # containing the `n` elements for which the block returns minimum values:
1079
+ #
1080
+ # (1..4).min_by(2) {|element| -element }
1081
+ # # => [4, 3]
1082
+ # %w[a b c d].min_by(2) {|element| -element.ord }
1083
+ # # => ["d", "c"]
1084
+ # {foo: 0, bar: 1, baz: 2}.min_by(2) {|key, value| -value }
1085
+ # # => [[:baz, 2], [:bar, 1]]
1086
+ # [].min_by(2) {|element| -element }
1087
+ # # => []
1088
+ #
1089
+ # Returns an Enumerator if no block is given.
1090
+ #
1091
+ # Related: #min, #minmax, #max_by.
1092
+ #
193
1093
  def min_by: () -> ::Enumerator[Elem, Elem?]
194
1094
  | () { (Elem arg0) -> (Comparable | ::Array[untyped]) } -> Elem?
195
1095
  | (Integer arg0) -> ::Enumerator[Elem, ::Array[Elem]]
196
1096
  | (Integer arg0) { (Elem arg0) -> (Comparable | ::Array[untyped]) } -> ::Array[Elem]
197
1097
 
198
- # Returns a two element array which contains the minimum and the maximum
199
- # value in the enumerable. The first form assumes all objects implement
200
- # `Comparable` ; the second uses the block to return *a \<=\> b* .
1098
+ # <!--
1099
+ # rdoc-file=enum.c
1100
+ # - minmax -> [minimum, maximum]
1101
+ # - minmax {|a, b| ... } -> [minimum, maximum]
1102
+ # -->
1103
+ # Returns a 2-element array containing the minimum and maximum elements
1104
+ # according to a given criterion. The ordering of equal elements is
1105
+ # indeterminate and may be unstable.
1106
+ #
1107
+ # With no argument and no block, returns the minimum and maximum elements, using
1108
+ # the elements' own method `<=>` for comparison:
1109
+ #
1110
+ # (1..4).minmax # => [1, 4]
1111
+ # (-4..-1).minmax # => [-4, -1]
1112
+ # %w[d c b a].minmax # => ["a", "d"]
1113
+ # {foo: 0, bar: 1, baz: 2}.minmax # => [[:bar, 1], [:foo, 0]]
1114
+ # [].minmax # => [nil, nil]
1115
+ #
1116
+ # With a block given, returns the minimum and maximum elements as determined by
1117
+ # the block:
1118
+ #
1119
+ # %w[xxx x xxxx xx].minmax {|a, b| a.size <=> b.size } # => ["x", "xxxx"]
1120
+ # h = {foo: 0, bar: 1, baz: 2}
1121
+ # h.minmax {|pair1, pair2| pair1[1] <=> pair2[1] }
1122
+ # # => [[:foo, 0], [:baz, 2]]
1123
+ # [].minmax {|a, b| a <=> b } # => [nil, nil]
1124
+ #
1125
+ # Related: #min, #max, #minmax_by.
201
1126
  #
202
- # ```ruby
203
- # a = %w(albatross dog horse)
204
- # a.minmax #=> ["albatross", "horse"]
205
- # a.minmax { |a, b| a.length <=> b.length } #=> ["dog", "albatross"]
206
- # ```
207
1127
  def minmax: () -> [ Elem?, Elem? ]
208
1128
  | () { (Elem arg0, Elem arg1) -> Integer } -> [ Elem?, Elem? ]
209
1129
 
1130
+ # <!--
1131
+ # rdoc-file=enum.c
1132
+ # - minmax_by {|element| ... } -> [minimum, maximum]
1133
+ # - minmax_by -> enumerator
1134
+ # -->
1135
+ # Returns a 2-element array containing the elements for which the block returns
1136
+ # minimum and maximum values:
1137
+ #
1138
+ # (1..4).minmax_by {|element| -element }
1139
+ # # => [4, 1]
1140
+ # %w[a b c d].minmax_by {|element| -element.ord }
1141
+ # # => ["d", "a"]
1142
+ # {foo: 0, bar: 1, baz: 2}.minmax_by {|key, value| -value }
1143
+ # # => [[:baz, 2], [:foo, 0]]
1144
+ # [].minmax_by {|element| -element }
1145
+ # # => [nil, nil]
1146
+ #
1147
+ # Returns an Enumerator if no block is given.
1148
+ #
1149
+ # Related: #max_by, #minmax, #min_by.
1150
+ #
210
1151
  def minmax_by: () -> [ Elem?, Elem? ]
211
1152
  | () { (Elem arg0) -> (Comparable | ::Array[untyped]) } -> [ Elem?, Elem? ]
212
1153
 
213
- # Passes each element of the collection to the given block. The method
214
- # returns `true` if the block never returns `true` for all elements. If
215
- # the block is not given, `none?` will return `true` only if none of the
216
- # collection members is true.
217
- #
218
- # If instead a pattern is supplied, the method returns whether `pattern
219
- # === element` for none of the collection members.
220
- #
221
- # ```ruby
222
- # %w{ant bear cat}.none? { |word| word.length == 5 } #=> true
223
- # %w{ant bear cat}.none? { |word| word.length >= 4 } #=> false
224
- # %w{ant bear cat}.none?(/d/) #=> true
225
- # [1, 3.14, 42].none?(Float) #=> false
226
- # [].none? #=> true
227
- # [nil].none? #=> true
228
- # [nil, false].none? #=> true
229
- # [nil, false, true].none? #=> false
230
- # ```
1154
+ # <!--
1155
+ # rdoc-file=enum.c
1156
+ # - none? -> true or false
1157
+ # - none?(pattern) -> true or false
1158
+ # - none? {|element| ... } -> true or false
1159
+ # -->
1160
+ # Returns whether no element meets a given criterion.
1161
+ #
1162
+ # With no argument and no block, returns whether no element is truthy:
1163
+ #
1164
+ # (1..4).none? # => false
1165
+ # [nil, false].none? # => true
1166
+ # {foo: 0}.none? # => false
1167
+ # {foo: 0, bar: 1}.none? # => false
1168
+ # [].none? # => true
1169
+ #
1170
+ # With argument `pattern` and no block, returns whether for no element
1171
+ # `element`, `pattern === element`:
1172
+ #
1173
+ # [nil, false, 1.1].none?(Integer) # => true
1174
+ # %w[bar baz bat bam].none?(/m/) # => false
1175
+ # %w[bar baz bat bam].none?(/foo/) # => true
1176
+ # %w[bar baz bat bam].none?('ba') # => true
1177
+ # {foo: 0, bar: 1, baz: 2}.none?(Hash) # => true
1178
+ # {foo: 0}.none?(Array) # => false
1179
+ # [].none?(Integer) # => true
1180
+ #
1181
+ # With a block given, returns whether the block returns a truthy value for no
1182
+ # element:
1183
+ #
1184
+ # (1..4).none? {|element| element < 1 } # => true
1185
+ # (1..4).none? {|element| element < 2 } # => false
1186
+ # {foo: 0, bar: 1, baz: 2}.none? {|key, value| value < 0 } # => true
1187
+ # {foo: 0, bar: 1, baz: 2}.none? {|key, value| value < 1 } # => false
1188
+ #
1189
+ # Related: #one?, #all?, #any?.
1190
+ #
231
1191
  def none?: () -> bool
232
1192
  | () { (Elem) -> boolish } -> bool
233
1193
 
234
- # Passes each element of the collection to the given block. The method
235
- # returns `true` if the block returns `true` exactly once. If the block is
236
- # not given, `one?` will return `true` only if exactly one of the
237
- # collection members is true.
238
- #
239
- # If instead a pattern is supplied, the method returns whether `pattern
240
- # === element` for exactly one collection member.
241
- #
242
- # ```ruby
243
- # %w{ant bear cat}.one? { |word| word.length == 4 } #=> true
244
- # %w{ant bear cat}.one? { |word| word.length > 4 } #=> false
245
- # %w{ant bear cat}.one? { |word| word.length < 4 } #=> false
246
- # %w{ant bear cat}.one?(/t/) #=> false
247
- # [ nil, true, 99 ].one? #=> false
248
- # [ nil, true, false ].one? #=> true
249
- # [ nil, true, 99 ].one?(Integer) #=> true
250
- # [].one? #=> false
251
- # ```
1194
+ # <!--
1195
+ # rdoc-file=enum.c
1196
+ # - one? -> true or false
1197
+ # - one?(pattern) -> true or false
1198
+ # - one? {|element| ... } -> true or false
1199
+ # -->
1200
+ # Returns whether exactly one element meets a given criterion.
1201
+ #
1202
+ # With no argument and no block, returns whether exactly one element is truthy:
1203
+ #
1204
+ # (1..1).one? # => true
1205
+ # [1, nil, false].one? # => true
1206
+ # (1..4).one? # => false
1207
+ # {foo: 0}.one? # => true
1208
+ # {foo: 0, bar: 1}.one? # => false
1209
+ # [].one? # => false
1210
+ #
1211
+ # With argument `pattern` and no block, returns whether for exactly one element
1212
+ # `element`, `pattern === element`:
1213
+ #
1214
+ # [nil, false, 0].one?(Integer) # => true
1215
+ # [nil, false, 0].one?(Numeric) # => true
1216
+ # [nil, false, 0].one?(Float) # => false
1217
+ # %w[bar baz bat bam].one?(/m/) # => true
1218
+ # %w[bar baz bat bam].one?(/foo/) # => false
1219
+ # %w[bar baz bat bam].one?('ba') # => false
1220
+ # {foo: 0, bar: 1, baz: 2}.one?(Array) # => false
1221
+ # {foo: 0}.one?(Array) # => true
1222
+ # [].one?(Integer) # => false
1223
+ #
1224
+ # With a block given, returns whether the block returns a truthy value for
1225
+ # exactly one element:
1226
+ #
1227
+ # (1..4).one? {|element| element < 2 } # => true
1228
+ # (1..4).one? {|element| element < 1 } # => false
1229
+ # {foo: 0, bar: 1, baz: 2}.one? {|key, value| value < 1 } # => true
1230
+ # {foo: 0, bar: 1, baz: 2}.one? {|key, value| value < 2 } # => false
1231
+ #
1232
+ # Related: #none?, #all?, #any?.
1233
+ #
252
1234
  def one?: () -> bool
253
1235
  | () { (Elem) -> boolish } -> bool
254
1236
 
1237
+ # <!--
1238
+ # rdoc-file=enum.c
1239
+ # - partition {|element| ... } -> [true_array, false_array]
1240
+ # - partition -> enumerator
1241
+ # -->
1242
+ # With a block given, returns an array of two arrays:
1243
+ #
1244
+ # * The first having those elements for which the block returns a truthy
1245
+ # value.
1246
+ # * The other having all other elements.
1247
+ #
1248
+ #
1249
+ # Examples:
1250
+ #
1251
+ # p = (1..4).partition {|i| i.even? }
1252
+ # p # => [[2, 4], [1, 3]]
1253
+ # p = ('a'..'d').partition {|c| c < 'c' }
1254
+ # p # => [["a", "b"], ["c", "d"]]
1255
+ # h = {foo: 0, bar: 1, baz: 2, bat: 3}
1256
+ # p = h.partition {|key, value| key.start_with?('b') }
1257
+ # p # => [[[:bar, 1], [:baz, 2], [:bat, 3]], [[:foo, 0]]]
1258
+ # p = h.partition {|key, value| value < 2 }
1259
+ # p # => [[[:foo, 0], [:bar, 1]], [[:baz, 2], [:bat, 3]]]
1260
+ #
1261
+ # With no block given, returns an Enumerator.
1262
+ #
1263
+ # Related: Enumerable#group_by.
1264
+ #
255
1265
  def partition: () { (Elem) -> boolish } -> [ ::Array[Elem], ::Array[Elem] ]
256
1266
  | () -> ::Enumerator[Elem, [ ::Array[Elem], ::Array[Elem] ]]
257
1267
 
1268
+ # <!--
1269
+ # rdoc-file=enum.c
1270
+ # - reject {|element| ... } -> array
1271
+ # - reject -> enumerator
1272
+ # -->
1273
+ # Returns an array of objects rejected by the block.
1274
+ #
1275
+ # With a block given, calls the block with successive elements; returns an array
1276
+ # of those elements for which the block returns `nil` or `false`:
1277
+ #
1278
+ # (0..9).reject {|i| i * 2 if i.even? } # => [1, 3, 5, 7, 9]
1279
+ # {foo: 0, bar: 1, baz: 2}.reject {|key, value| key if value.odd? } # => {:foo=>0, :baz=>2}
1280
+ #
1281
+ # When no block given, returns an Enumerator.
1282
+ #
1283
+ # Related: #select.
1284
+ #
258
1285
  def reject: () { (Elem) -> boolish } -> ::Array[Elem]
259
1286
  | () -> ::Enumerator[Elem, ::Array[Elem]]
260
1287
 
1288
+ # <!--
1289
+ # rdoc-file=enum.c
1290
+ # - reverse_each(*args) {|element| ... } -> self
1291
+ # - reverse_each(*args) -> enumerator
1292
+ # -->
1293
+ # With a block given, calls the block with each element, but in reverse order;
1294
+ # returns `self`:
1295
+ #
1296
+ # a = []
1297
+ # (1..4).reverse_each {|element| a.push(-element) } # => 1..4
1298
+ # a # => [-4, -3, -2, -1]
1299
+ #
1300
+ # a = []
1301
+ # %w[a b c d].reverse_each {|element| a.push(element) }
1302
+ # # => ["a", "b", "c", "d"]
1303
+ # a # => ["d", "c", "b", "a"]
1304
+ #
1305
+ # a = []
1306
+ # h.reverse_each {|element| a.push(element) }
1307
+ # # => {:foo=>0, :bar=>1, :baz=>2}
1308
+ # a # => [[:baz, 2], [:bar, 1], [:foo, 0]]
1309
+ #
1310
+ # With no block given, returns an Enumerator.
1311
+ #
261
1312
  def reverse_each: () { (Elem arg0) -> untyped } -> void
262
1313
  | () -> ::Enumerator[Elem, void]
263
1314
 
264
- # Returns an array containing the items in *enum* sorted.
1315
+ # <!--
1316
+ # rdoc-file=enum.c
1317
+ # - sort -> array
1318
+ # - sort {|a, b| ... } -> array
1319
+ # -->
1320
+ # Returns an array containing the sorted elements of `self`. The ordering of
1321
+ # equal elements is indeterminate and may be unstable.
1322
+ #
1323
+ # With no block given, the sort compares using the elements' own method `<=>`:
1324
+ #
1325
+ # %w[b c a d].sort # => ["a", "b", "c", "d"]
1326
+ # {foo: 0, bar: 1, baz: 2}.sort # => [[:bar, 1], [:baz, 2], [:foo, 0]]
265
1327
  #
266
- # Comparisons for the sort will be done using the items’ own `<=>`
267
- # operator or using an optional code block.
1328
+ # With a block given, comparisons in the block determine the ordering. The block
1329
+ # is called with two elements `a` and `b`, and must return:
268
1330
  #
269
- # The block must implement a comparison between `a` and `b` and return an
270
- # integer less than 0 when `b` follows `a`, `0` when `a` and `b` are
271
- # equivalent, or an integer greater than 0 when `a` follows `b` .
1331
+ # * A negative integer if `a < b`.
1332
+ # * Zero if `a == b`.
1333
+ # * A positive integer if `a > b`.
272
1334
  #
273
- # The result is not guaranteed to be stable. When the comparison of two
274
- # elements returns `0`, the order of the elements is unpredictable.
275
1335
  #
276
- # ```ruby
277
- # %w(rhea kea flea).sort #=> ["flea", "kea", "rhea"]
278
- # (1..10).sort { |a, b| b <=> a } #=> [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
279
- # ```
1336
+ # Examples:
1337
+ #
1338
+ # a = %w[b c a d]
1339
+ # a.sort {|a, b| b <=> a } # => ["d", "c", "b", "a"]
1340
+ # h = {foo: 0, bar: 1, baz: 2}
1341
+ # h.sort {|a, b| b <=> a } # => [[:foo, 0], [:baz, 2], [:bar, 1]]
1342
+ #
1343
+ # See also #sort_by. It implements a Schwartzian transform which is useful when
1344
+ # key computation or comparison is expensive.
280
1345
  #
281
- # See also [\#sort\_by](Enumerable.downloaded.ruby_doc#method-i-sort_by).
282
- # It implements a Schwartzian transform which is useful when key
283
- # computation or comparison is expensive.
284
1346
  def sort: () -> ::Array[Elem]
285
1347
  | () { (Elem arg0, Elem arg1) -> Integer } -> ::Array[Elem]
286
1348
 
1349
+ # <!--
1350
+ # rdoc-file=enum.c
1351
+ # - sort_by {|element| ... } -> array
1352
+ # - sort_by -> enumerator
1353
+ # -->
1354
+ # With a block given, returns an array of elements of `self`, sorted according
1355
+ # to the value returned by the block for each element. The ordering of equal
1356
+ # elements is indeterminate and may be unstable.
1357
+ #
1358
+ # Examples:
1359
+ #
1360
+ # a = %w[xx xxx x xxxx]
1361
+ # a.sort_by {|s| s.size } # => ["x", "xx", "xxx", "xxxx"]
1362
+ # a.sort_by {|s| -s.size } # => ["xxxx", "xxx", "xx", "x"]
1363
+ # h = {foo: 2, bar: 1, baz: 0}
1364
+ # h.sort_by{|key, value| value } # => [[:baz, 0], [:bar, 1], [:foo, 2]]
1365
+ # h.sort_by{|key, value| key } # => [[:bar, 1], [:baz, 0], [:foo, 2]]
1366
+ #
1367
+ # With no block given, returns an Enumerator.
1368
+ #
1369
+ # The current implementation of #sort_by generates an array of tuples containing
1370
+ # the original collection element and the mapped value. This makes #sort_by
1371
+ # fairly expensive when the keysets are simple.
1372
+ #
1373
+ # require 'benchmark'
1374
+ #
1375
+ # a = (1..100000).map { rand(100000) }
1376
+ #
1377
+ # Benchmark.bm(10) do |b|
1378
+ # b.report("Sort") { a.sort }
1379
+ # b.report("Sort by") { a.sort_by { |a| a } }
1380
+ # end
1381
+ #
1382
+ # *produces:*
1383
+ #
1384
+ # user system total real
1385
+ # Sort 0.180000 0.000000 0.180000 ( 0.175469)
1386
+ # Sort by 1.980000 0.040000 2.020000 ( 2.013586)
1387
+ #
1388
+ # However, consider the case where comparing the keys is a non-trivial
1389
+ # operation. The following code sorts some files on modification time using the
1390
+ # basic #sort method.
1391
+ #
1392
+ # files = Dir["*"]
1393
+ # sorted = files.sort { |a, b| File.new(a).mtime <=> File.new(b).mtime }
1394
+ # sorted #=> ["mon", "tues", "wed", "thurs"]
1395
+ #
1396
+ # This sort is inefficient: it generates two new File objects during every
1397
+ # comparison. A slightly better technique is to use the Kernel#test method to
1398
+ # generate the modification times directly.
1399
+ #
1400
+ # files = Dir["*"]
1401
+ # sorted = files.sort { |a, b|
1402
+ # test(?M, a) <=> test(?M, b)
1403
+ # }
1404
+ # sorted #=> ["mon", "tues", "wed", "thurs"]
1405
+ #
1406
+ # This still generates many unnecessary Time objects. A more efficient technique
1407
+ # is to cache the sort keys (modification times in this case) before the sort.
1408
+ # Perl users often call this approach a Schwartzian transform, after Randal
1409
+ # Schwartz. We construct a temporary array, where each element is an array
1410
+ # containing our sort key along with the filename. We sort this array, and then
1411
+ # extract the filename from the result.
1412
+ #
1413
+ # sorted = Dir["*"].collect { |f|
1414
+ # [test(?M, f), f]
1415
+ # }.sort.collect { |f| f[1] }
1416
+ # sorted #=> ["mon", "tues", "wed", "thurs"]
1417
+ #
1418
+ # This is exactly what #sort_by does internally.
1419
+ #
1420
+ # sorted = Dir["*"].sort_by { |f| test(?M, f) }
1421
+ # sorted #=> ["mon", "tues", "wed", "thurs"]
1422
+ #
1423
+ # To produce the reverse of a specific order, the following can be used:
1424
+ #
1425
+ # ary.sort_by { ... }.reverse!
1426
+ #
287
1427
  def sort_by: () { (Elem arg0) -> (Comparable | ::Array[untyped]) } -> ::Array[Elem]
288
1428
  | () -> ::Enumerator[Elem, ::Array[Elem]]
289
1429
 
290
- # Returns first n elements from *enum*.
1430
+ # <!--
1431
+ # rdoc-file=enum.c
1432
+ # - take(n) -> array
1433
+ # -->
1434
+ # For non-negative integer `n`, returns the first `n` elements:
291
1435
  #
292
- # a = [1, 2, 3, 4, 5, 0]
293
- # a.take(3) #=> [1, 2, 3]
294
- # a.take(30) #=> [1, 2, 3, 4, 5, 0]
1436
+ # r = (1..4)
1437
+ # r.take(2) # => [1, 2]
1438
+ # r.take(0) # => []
1439
+ #
1440
+ # h = {foo: 0, bar: 1, baz: 2, bat: 3}
1441
+ # h.take(2) # => [[:foo, 0], [:bar, 1]]
295
1442
  #
296
1443
  def take: (Integer n) -> ::Array[Elem]
297
1444
 
298
- # Passes elements to the block until the block returns `nil` or `false`, then
299
- # stops iterating and returns an array of all prior elements.
1445
+ # <!--
1446
+ # rdoc-file=enum.c
1447
+ # - take_while {|element| ... } -> array
1448
+ # - take_while -> enumerator
1449
+ # -->
1450
+ # Calls the block with successive elements as long as the block returns a truthy
1451
+ # value; returns an array of all elements up to that point:
300
1452
  #
301
- # If no block is given, an enumerator is returned instead.
1453
+ # (1..4).take_while{|i| i < 3 } # => [1, 2]
1454
+ # h = {foo: 0, bar: 1, baz: 2}
1455
+ # h.take_while{|element| key, value = *element; value < 2 }
1456
+ # # => [[:foo, 0], [:bar, 1]]
302
1457
  #
303
- # a = [1, 2, 3, 4, 5, 0]
304
- # a.take_while { |i| i < 3 } #=> [1, 2]
1458
+ # With no block given, returns an Enumerator.
305
1459
  #
306
1460
  def take_while: () { (Elem) -> boolish } -> ::Array[Elem]
307
1461
  | () -> ::Enumerator[Elem, ::Array[Elem]]
308
1462
 
309
- # Implemented in C++
310
- # Returns the result of interpreting *enum* as a list of `[key, value]`
311
- # pairs.
1463
+ # <!--
1464
+ # rdoc-file=enum.c
1465
+ # - to_h -> hash
1466
+ # - to_h {|element| ... } -> hash
1467
+ # -->
1468
+ # When `self` consists of 2-element arrays, returns a hash each of whose entries
1469
+ # is the key-value pair formed from one of those arrays:
1470
+ #
1471
+ # [[:foo, 0], [:bar, 1], [:baz, 2]].to_h # => {:foo=>0, :bar=>1, :baz=>2}
312
1472
  #
313
- # %i[hello world].each_with_index.to_h
314
- # # => {:hello => 0, :world => 1}
1473
+ # When a block is given, the block is called with each element of `self`; the
1474
+ # block should return a 2-element array which becomes a key-value pair in the
1475
+ # returned hash:
315
1476
  #
316
- # If a block is given, the results of the block on each element of the
317
- # enum will be used as pairs.
1477
+ # (0..3).to_h {|i| [i, i ** 2]} # => {0=>0, 1=>1, 2=>4, 3=>9}
1478
+ #
1479
+ # Raises an exception if an element of `self` is not a 2-element array, and a
1480
+ # block is not passed.
318
1481
  #
319
- # ```ruby
320
- # (1..5).to_h {|x| [x, x ** 2]}
321
- # #=> {1=>1, 2=>4, 3=>9, 4=>16, 5=>25}
322
- # ```
323
1482
  def to_h: () -> ::Hash[untyped, untyped]
324
- | [T, U] () { (Elem) -> [T, U] } -> ::Hash[T, U]
1483
+ | [T, U] () { (Elem) -> [ T, U ] } -> ::Hash[T, U]
325
1484
 
326
- def each_slice: (Integer n) { (::Array[Elem]) -> untyped } -> NilClass
327
- | (Integer n) -> ::Enumerator[::Array[Elem], NilClass]
1485
+ # <!--
1486
+ # rdoc-file=enum.c
1487
+ # - each_slice(n) { ... } -> self
1488
+ # - each_slice(n) -> enumerator
1489
+ # -->
1490
+ # Calls the block with each successive disjoint `n`-tuple of elements; returns
1491
+ # `self`:
1492
+ #
1493
+ # a = []
1494
+ # (1..10).each_slice(3) {|tuple| a.push(tuple) }
1495
+ # a # => [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]
1496
+ #
1497
+ # a = []
1498
+ # h = {foo: 0, bar: 1, baz: 2, bat: 3, bam: 4}
1499
+ # h.each_slice(2) {|tuple| a.push(tuple) }
1500
+ # a # => [[[:foo, 0], [:bar, 1]], [[:baz, 2], [:bat, 3]], [[:bam, 4]]]
1501
+ #
1502
+ # With no block given, returns an Enumerator.
1503
+ #
1504
+ def each_slice: (Integer n) { (::Array[Elem]) -> void } -> self
1505
+ | (Integer n) -> ::Enumerator[::Array[Elem], self]
328
1506
 
329
1507
  interface _NotFound[T]
330
1508
  def call: () -> T
331
1509
  end
332
1510
 
1511
+ # <!--
1512
+ # rdoc-file=enum.c
1513
+ # - find(if_none_proc = nil) {|element| ... } -> object or nil
1514
+ # - find(if_none_proc = nil) -> enumerator
1515
+ # -->
1516
+ # Returns the first element for which the block returns a truthy value.
1517
+ #
1518
+ # With a block given, calls the block with successive elements of the
1519
+ # collection; returns the first element for which the block returns a truthy
1520
+ # value:
1521
+ #
1522
+ # (0..9).find {|element| element > 2} # => 3
1523
+ #
1524
+ # If no such element is found, calls `if_none_proc` and returns its return
1525
+ # value.
1526
+ #
1527
+ # (0..9).find(proc {false}) {|element| element > 12} # => false
1528
+ # {foo: 0, bar: 1, baz: 2}.find {|key, value| key.start_with?('b') } # => [:bar, 1]
1529
+ # {foo: 0, bar: 1, baz: 2}.find(proc {[]}) {|key, value| key.start_with?('c') } # => []
1530
+ #
1531
+ # With no block given, returns an Enumerator.
1532
+ #
333
1533
  def find: () { (Elem) -> boolish } -> Elem?
334
1534
  | () -> ::Enumerator[Elem, Elem?]
335
1535
  | [T] (_NotFound[T] ifnone) { (Elem) -> boolish } -> (Elem | T)
336
1536
  | [T] (_NotFound[T] ifnone) -> ::Enumerator[Elem, Elem | T]
337
1537
 
1538
+ # <!--
1539
+ # rdoc-file=enum.c
1540
+ # - flat_map {|element| ... } -> array
1541
+ # - flat_map -> enumerator
1542
+ # -->
1543
+ # Returns an array of flattened objects returned by the block.
1544
+ #
1545
+ # With a block given, calls the block with successive elements; returns a
1546
+ # flattened array of objects returned by the block:
1547
+ #
1548
+ # [0, 1, 2, 3].flat_map {|element| -element } # => [0, -1, -2, -3]
1549
+ # [0, 1, 2, 3].flat_map {|element| [element, -element] } # => [0, 0, 1, -1, 2, -2, 3, -3]
1550
+ # [[0, 1], [2, 3]].flat_map {|e| e + [100] } # => [0, 1, 100, 2, 3, 100]
1551
+ # {foo: 0, bar: 1, baz: 2}.flat_map {|key, value| [key, value] } # => [:foo, 0, :bar, 1, :baz, 2]
1552
+ #
1553
+ # With no block given, returns an Enumerator.
1554
+ #
1555
+ # Alias: #collect_concat.
1556
+ #
338
1557
  def flat_map: [U] () { (Elem) -> (Array[U] | U) } -> Array[U]
339
1558
  | () -> ::Enumerator[Elem, Array[untyped]]
340
1559
 
1560
+ # <!-- rdoc-file=enum.c -->
1561
+ # Returns an array of objects returned by the block.
1562
+ #
1563
+ # With a block given, calls the block with successive elements; returns an array
1564
+ # of the objects returned by the block:
1565
+ #
1566
+ # (0..4).map {|i| i*i } # => [0, 1, 4, 9, 16]
1567
+ # {foo: 0, bar: 1, baz: 2}.map {|key, value| value*2} # => [0, 2, 4]
1568
+ #
1569
+ # With no block given, returns an Enumerator.
1570
+ #
341
1571
  def map: [U] () { (Elem arg0) -> U } -> ::Array[U]
342
1572
  | () -> ::Enumerator[Elem, ::Array[untyped]]
343
1573
 
1574
+ # <!--
1575
+ # rdoc-file=enum.c
1576
+ # - include?(object) -> true or false
1577
+ # -->
1578
+ # Returns whether for any element `object == element`:
1579
+ #
1580
+ # (1..4).include?(2) # => true
1581
+ # (1..4).include?(5) # => false
1582
+ # (1..4).include?('2') # => false
1583
+ # %w[a b c d].include?('b') # => true
1584
+ # %w[a b c d].include?('2') # => false
1585
+ # {foo: 0, bar: 1, baz: 2}.include?(:foo) # => true
1586
+ # {foo: 0, bar: 1, baz: 2}.include?('foo') # => false
1587
+ # {foo: 0, bar: 1, baz: 2}.include?(0) # => false
1588
+ #
1589
+ # Enumerable#member? is an alias for Enumerable#include?.
1590
+ #
344
1591
  def member?: (Elem arg0) -> bool
345
1592
 
1593
+ # <!-- rdoc-file=enum.c -->
1594
+ # Returns an object formed from operands via either:
1595
+ #
1596
+ # * A method named by `symbol`.
1597
+ # * A block to which each operand is passed.
1598
+ #
1599
+ #
1600
+ # With method-name argument `symbol`, combines operands using the method:
1601
+ #
1602
+ # # Sum, without initial_operand.
1603
+ # (1..4).inject(:+) # => 10
1604
+ # # Sum, with initial_operand.
1605
+ # (1..4).inject(10, :+) # => 20
1606
+ #
1607
+ # With a block, passes each operand to the block:
1608
+ #
1609
+ # # Sum of squares, without initial_operand.
1610
+ # (1..4).inject {|sum, n| sum + n*n } # => 30
1611
+ # # Sum of squares, with initial_operand.
1612
+ # (1..4).inject(2) {|sum, n| sum + n*n } # => 32
1613
+ #
1614
+ # **Operands**
1615
+ #
1616
+ # If argument `initial_operand` is not given, the operands for `inject` are
1617
+ # simply the elements of `self`. Example calls and their operands:
1618
+ #
1619
+ # `(1..4).inject(:+)`
1620
+ # : `[1, 2, 3, 4]`.
1621
+ #
1622
+ # `(1...4).inject(:+)`
1623
+ # : `[1, 2, 3]`.
1624
+ #
1625
+ # `('a'..'d').inject(:+)`
1626
+ # : `['a', 'b', 'c', 'd']`.
1627
+ #
1628
+ # `('a'...'d').inject(:+)`
1629
+ # : `['a', 'b', 'c']`.
1630
+ #
1631
+ #
1632
+ #
1633
+ # Examples with first operand (which is `self.first`) of various types:
1634
+ #
1635
+ # # Integer.
1636
+ # (1..4).inject(:+) # => 10
1637
+ # # Float.
1638
+ # [1.0, 2, 3, 4].inject(:+) # => 10.0
1639
+ # # Character.
1640
+ # ('a'..'d').inject(:+) # => "abcd"
1641
+ # # Complex.
1642
+ # [Complex(1, 2), 3, 4].inject(:+) # => (8+2i)
1643
+ #
1644
+ # If argument `initial_operand` is given, the operands for `inject` are that
1645
+ # value plus the elements of `self`. Example calls their operands:
1646
+ #
1647
+ # `(1..4).inject(10, :+)`
1648
+ # : `[10, 1, 2, 3, 4]`.
1649
+ #
1650
+ # `(1...4).inject(10, :+)`
1651
+ # : `[10, 1, 2, 3]`.
1652
+ #
1653
+ # `('a'..'d').inject('e', :+)`
1654
+ # : `['e', 'a', 'b', 'c', 'd']`.
1655
+ #
1656
+ # `('a'...'d').inject('e', :+)`
1657
+ # : `['e', 'a', 'b', 'c']`.
1658
+ #
1659
+ #
1660
+ #
1661
+ # Examples with `initial_operand` of various types:
1662
+ #
1663
+ # # Integer.
1664
+ # (1..4).inject(2, :+) # => 12
1665
+ # # Float.
1666
+ # (1..4).inject(2.0, :+) # => 12.0
1667
+ # # String.
1668
+ # ('a'..'d').inject('foo', :+) # => "fooabcd"
1669
+ # # Array.
1670
+ # %w[a b c].inject(['x'], :push) # => ["x", "a", "b", "c"]
1671
+ # # Complex.
1672
+ # (1..4).inject(Complex(2, 2), :+) # => (12+2i)
1673
+ #
1674
+ # **Combination by Given \Method**
1675
+ #
1676
+ # If the method-name argument `symbol` is given, the operands are combined by
1677
+ # that method:
1678
+ #
1679
+ # * The first and second operands are combined.
1680
+ # * That result is combined with the third operand.
1681
+ # * That result is combined with the fourth operand.
1682
+ # * And so on.
1683
+ #
1684
+ #
1685
+ # The return value from `inject` is the result of the last combination.
1686
+ #
1687
+ # This call to `inject` computes the sum of the operands:
1688
+ #
1689
+ # (1..4).inject(:+) # => 10
1690
+ #
1691
+ # Examples with various methods:
1692
+ #
1693
+ # # Integer addition.
1694
+ # (1..4).inject(:+) # => 10
1695
+ # # Integer multiplication.
1696
+ # (1..4).inject(:*) # => 24
1697
+ # # Character range concatenation.
1698
+ # ('a'..'d').inject('', :+) # => "abcd"
1699
+ # # String array concatenation.
1700
+ # %w[foo bar baz].inject('', :+) # => "foobarbaz"
1701
+ # # Hash update.
1702
+ # h = [{foo: 0, bar: 1}, {baz: 2}, {bat: 3}].inject(:update)
1703
+ # h # => {:foo=>0, :bar=>1, :baz=>2, :bat=>3}
1704
+ # # Hash conversion to nested arrays.
1705
+ # h = {foo: 0, bar: 1}.inject([], :push)
1706
+ # h # => [[:foo, 0], [:bar, 1]]
1707
+ #
1708
+ # **Combination by Given Block**
1709
+ #
1710
+ # If a block is given, the operands are passed to the block:
1711
+ #
1712
+ # * The first call passes the first and second operands.
1713
+ # * The second call passes the result of the first call, along with the third
1714
+ # operand.
1715
+ # * The third call passes the result of the second call, along with the fourth
1716
+ # operand.
1717
+ # * And so on.
1718
+ #
1719
+ #
1720
+ # The return value from `inject` is the return value from the last block call.
1721
+ #
1722
+ # This call to `inject` gives a block that writes the memo and element, and also
1723
+ # sums the elements:
1724
+ #
1725
+ # (1..4).inject do |memo, element|
1726
+ # p "Memo: #{memo}; element: #{element}"
1727
+ # memo + element
1728
+ # end # => 10
1729
+ #
1730
+ # Output:
1731
+ #
1732
+ # "Memo: 1; element: 2"
1733
+ # "Memo: 3; element: 3"
1734
+ # "Memo: 6; element: 4"
1735
+ #
1736
+ # Enumerable#reduce is an alias for Enumerable#inject.
1737
+ #
346
1738
  alias reduce inject
347
1739
 
348
- # Returns an array containing the items in *enum* .
1740
+ # <!--
1741
+ # rdoc-file=enum.c
1742
+ # - to_a -> array
1743
+ # -->
1744
+ # Returns an array containing the items in `self`:
1745
+ #
1746
+ # (0..4).to_a # => [0, 1, 2, 3, 4]
349
1747
  #
350
- # ```ruby
351
- # (1..7).to_a #=> [1, 2, 3, 4, 5, 6, 7]
352
- # { 'a'=>1, 'b'=>2, 'c'=>3 }.to_a #=> [["a", 1], ["b", 2], ["c", 3]]
1748
+ # Enumerable#entries is an alias for Enumerable#to_a.
353
1749
  #
354
- # require 'prime'
355
- # Prime.entries 10 #=> [2, 3, 5, 7]
356
- # ```
357
1750
  def to_a: () -> ::Array[Elem]
358
1751
 
359
- # Returns a lazy enumerator, whose methods map/collect,
360
- # flat\_map/collect\_concat, select/find\_all, reject, grep,
361
- # [\#grep\_v](Enumerable.downloaded.ruby_doc#method-i-grep_v), zip, take,
362
- # [\#take\_while](Enumerable.downloaded.ruby_doc#method-i-take_while),
363
- # drop, and
364
- # [\#drop\_while](Enumerable.downloaded.ruby_doc#method-i-drop_while)
365
- # enumerate values only on an as-needed basis. However, if a block is
366
- # given to zip, values are enumerated immediately.
1752
+ # <!--
1753
+ # rdoc-file=enumerator.c
1754
+ # - e.lazy -> lazy_enumerator
1755
+ # -->
1756
+ # Returns an Enumerator::Lazy, which redefines most Enumerable methods to
1757
+ # postpone enumeration and enumerate values only on an as-needed basis.
367
1758
  #
1759
+ # ### Example
368
1760
  #
369
1761
  # The following program finds pythagorean triples:
370
1762
  #
371
- # ```ruby
372
- # def pythagorean_triples
373
- # (1..Float::INFINITY).lazy.flat_map {|z|
374
- # (1..z).flat_map {|x|
375
- # (x..z).select {|y|
376
- # x**2 + y**2 == z**2
377
- # }.map {|y|
378
- # [x, y, z]
1763
+ # def pythagorean_triples
1764
+ # (1..Float::INFINITY).lazy.flat_map {|z|
1765
+ # (1..z).flat_map {|x|
1766
+ # (x..z).select {|y|
1767
+ # x**2 + y**2 == z**2
1768
+ # }.map {|y|
1769
+ # [x, y, z]
1770
+ # }
1771
+ # }
379
1772
  # }
380
- # }
381
- # }
382
- # end
383
- # # show first ten pythagorean triples
384
- # p pythagorean_triples.take(10).force # take is lazy, so force is needed
385
- # p pythagorean_triples.first(10) # first is eager
386
- # # show pythagorean triples less than 100
387
- # p pythagorean_triples.take_while { |*, z| z < 100 }.force
388
- # ```
1773
+ # end
1774
+ # # show first ten pythagorean triples
1775
+ # p pythagorean_triples.take(10).force # take is lazy, so force is needed
1776
+ # p pythagorean_triples.first(10) # first is eager
1777
+ # # show pythagorean triples less than 100
1778
+ # p pythagorean_triples.take_while { |*, z| z < 100 }.force
1779
+ #
389
1780
  def lazy: () -> Enumerator::Lazy[Elem, void]
390
1781
 
1782
+ # <!--
1783
+ # rdoc-file=enum.c
1784
+ # - uniq -> array
1785
+ # - uniq {|element| ... } -> array
1786
+ # -->
1787
+ # With no block, returns a new array containing only unique elements; the array
1788
+ # has no two elements `e0` and `e1` such that `e0.eql?(e1)`:
1789
+ #
1790
+ # %w[a b c c b a a b c].uniq # => ["a", "b", "c"]
1791
+ # [0, 1, 2, 2, 1, 0, 0, 1, 2].uniq # => [0, 1, 2]
1792
+ #
1793
+ # With a block, returns a new array containing only for which the block returns
1794
+ # a unique value:
1795
+ #
1796
+ # a = [0, 1, 2, 3, 4, 5, 5, 4, 3, 2, 1]
1797
+ # a.uniq {|i| i.even? ? i : 0 } # => [0, 2, 4]
1798
+ # a = %w[a b c d e e d c b a a b c d e]
1799
+ # a.uniq {|c| c < 'c' } # => ["a", "c"]
1800
+ #
391
1801
  def uniq: () -> ::Array[Elem]
392
1802
  | () { (Elem item) -> untyped } -> ::Array[Elem]
393
1803
 
1804
+ # <!--
1805
+ # rdoc-file=enum.c
1806
+ # - sum(initial_value = 0) -> number
1807
+ # - sum(initial_value = 0) {|element| ... } -> object
1808
+ # -->
1809
+ # With no block given, returns the sum of `initial_value` and the elements:
1810
+ #
1811
+ # (1..100).sum # => 5050
1812
+ # (1..100).sum(1) # => 5051
1813
+ # ('a'..'d').sum('foo') # => "fooabcd"
1814
+ #
1815
+ # Generally, the sum is computed using methods `+` and `each`; for performance
1816
+ # optimizations, those methods may not be used, and so any redefinition of those
1817
+ # methods may not have effect here.
1818
+ #
1819
+ # One such optimization: When possible, computes using Gauss's summation formula
1820
+ # *n(n+1)/2*:
1821
+ #
1822
+ # 100 * (100 + 1) / 2 # => 5050
1823
+ #
1824
+ # With a block given, calls the block with each element; returns the sum of
1825
+ # `initial_value` and the block return values:
1826
+ #
1827
+ # (1..4).sum {|i| i*i } # => 30
1828
+ # (1..4).sum(100) {|i| i*i } # => 130
1829
+ # h = {a: 0, b: 1, c: 2, d: 3, e: 4, f: 5}
1830
+ # h.sum {|key, value| value.odd? ? value : 0 } # => 9
1831
+ # ('a'..'f').sum('x') {|c| c < 'd' ? c : '' } # => "xabc"
1832
+ #
394
1833
  def sum: () -> (Elem | Integer)
395
1834
  | [T] () { (Elem arg0) -> T } -> (Integer | T)
396
1835
  | [T] (?T arg0) -> (Elem | T)
397
1836
  | [U] (?U arg0) { (Elem arg0) -> U } -> U
398
1837
 
1838
+ # <!--
1839
+ # rdoc-file=enum.c
1840
+ # - filter_map {|element| ... } -> array
1841
+ # - filter_map -> enumerator
1842
+ # -->
1843
+ # Returns an array containing truthy elements returned by the block.
1844
+ #
1845
+ # With a block given, calls the block with successive elements; returns an array
1846
+ # containing each truthy value returned by the block:
1847
+ #
1848
+ # (0..9).filter_map {|i| i * 2 if i.even? } # => [0, 4, 8, 12, 16]
1849
+ # {foo: 0, bar: 1, baz: 2}.filter_map {|key, value| key if value.even? } # => [:foo, :baz]
1850
+ #
1851
+ # When no block given, returns an Enumerator.
1852
+ #
399
1853
  def filter_map: [U] () { (Elem elem) -> (nil | false | U) } -> ::Array[U]
400
1854
  | () -> ::Enumerator[Elem, ::Array[untyped]]
401
1855
 
1856
+ # <!--
1857
+ # rdoc-file=enumerator.c
1858
+ # - e.chain(*enums) -> enumerator
1859
+ # -->
1860
+ # Returns an enumerator object generated from this enumerator and given
1861
+ # enumerables.
1862
+ #
1863
+ # e = (1..3).chain([4, 5])
1864
+ # e.to_a #=> [1, 2, 3, 4, 5]
1865
+ #
402
1866
  def chain: (*self enumerables) -> ::Enumerator::Chain[Elem]
403
1867
 
404
- def tally: () -> ::Hash[Elem, Integer]
1868
+ # <!--
1869
+ # rdoc-file=enum.c
1870
+ # - tally -> new_hash
1871
+ # - tally(hash) -> hash
1872
+ # -->
1873
+ # Returns a hash containing the counts of equal elements:
1874
+ #
1875
+ # * Each key is an element of `self`.
1876
+ # * Each value is the number elements equal to that key.
1877
+ #
1878
+ #
1879
+ # With no argument:
1880
+ #
1881
+ # %w[a b c b c a c b].tally # => {"a"=>2, "b"=>3, "c"=>3}
1882
+ #
1883
+ # With a hash argument, that hash is used for the tally (instead of a new hash),
1884
+ # and is returned; this may be useful for accumulating tallies across multiple
1885
+ # enumerables:
1886
+ #
1887
+ # hash = {}
1888
+ # hash = %w[a c d b c a].tally(hash)
1889
+ # hash # => {"a"=>2, "c"=>2, "d"=>1, "b"=>1}
1890
+ # hash = %w[b a z].tally(hash)
1891
+ # hash # => {"a"=>3, "c"=>2, "d"=>1, "b"=>2, "z"=>1}
1892
+ # hash = %w[b a m].tally(hash)
1893
+ # hash # => {"a"=>4, "c"=>2, "d"=>1, "b"=>3, "z"=>1, "m"=> 1}
1894
+ #
1895
+ def tally: (?Hash[Elem, Integer] hash) -> ::Hash[Elem, Integer]
405
1896
 
1897
+ # <!--
1898
+ # rdoc-file=enum.c
1899
+ # - each_entry(*args) {|element| ... } -> self
1900
+ # - each_entry(*args) -> enumerator
1901
+ # -->
1902
+ # Calls the given block with each element, converting multiple values from yield
1903
+ # to an array; returns `self`:
1904
+ #
1905
+ # a = []
1906
+ # (1..4).each_entry {|element| a.push(element) } # => 1..4
1907
+ # a # => [1, 2, 3, 4]
1908
+ #
1909
+ # a = []
1910
+ # h = {foo: 0, bar: 1, baz:2}
1911
+ # h.each_entry {|element| a.push(element) }
1912
+ # # => {:foo=>0, :bar=>1, :baz=>2}
1913
+ # a # => [[:foo, 0], [:bar, 1], [:baz, 2]]
1914
+ #
1915
+ # class Foo
1916
+ # include Enumerable
1917
+ # def each
1918
+ # yield 1
1919
+ # yield 1, 2
1920
+ # yield
1921
+ # end
1922
+ # end
1923
+ # Foo.new.each_entry {|yielded| p yielded }
1924
+ #
1925
+ # Output:
1926
+ #
1927
+ # 1
1928
+ # [1, 2]
1929
+ # nil
1930
+ #
1931
+ # With no block given, returns an Enumerator.
1932
+ #
406
1933
  def each_entry: () -> ::Enumerator[Elem, self]
407
1934
  | () { (Elem arg0) -> untyped } -> self
408
1935
 
409
- # variadic type parameter is not supported yet
410
- # https://github.com/ruby/rbs/issues/21
411
- def zip: [Elem2] (::Enumerable[Elem2] enum) -> ::Array[[Elem, Elem2 | nil]]
412
- | [U, Elem2] (::Enumerable[Elem2]) { ([Elem, Elem2 | nil]) -> U } -> nil
1936
+ # <!--
1937
+ # rdoc-file=enum.c
1938
+ # - zip(*other_enums) -> array
1939
+ # - zip(*other_enums) {|array| ... } -> nil
1940
+ # -->
1941
+ # With no block given, returns a new array `new_array` of size self.size whose
1942
+ # elements are arrays. Each nested array `new_array[n]` is of size
1943
+ # `other_enums.size+1`, and contains:
1944
+ #
1945
+ # * The `n`-th element of self.
1946
+ # * The `n`-th element of each of the `other_enums`.
1947
+ #
1948
+ #
1949
+ # If all `other_enums` and self are the same size, all elements are included in
1950
+ # the result, and there is no `nil`-filling:
1951
+ #
1952
+ # a = [:a0, :a1, :a2, :a3]
1953
+ # b = [:b0, :b1, :b2, :b3]
1954
+ # c = [:c0, :c1, :c2, :c3]
1955
+ # d = a.zip(b, c)
1956
+ # d # => [[:a0, :b0, :c0], [:a1, :b1, :c1], [:a2, :b2, :c2], [:a3, :b3, :c3]]
1957
+ #
1958
+ # f = {foo: 0, bar: 1, baz: 2}
1959
+ # g = {goo: 3, gar: 4, gaz: 5}
1960
+ # h = {hoo: 6, har: 7, haz: 8}
1961
+ # d = f.zip(g, h)
1962
+ # d # => [
1963
+ # # [[:foo, 0], [:goo, 3], [:hoo, 6]],
1964
+ # # [[:bar, 1], [:gar, 4], [:har, 7]],
1965
+ # # [[:baz, 2], [:gaz, 5], [:haz, 8]]
1966
+ # # ]
1967
+ #
1968
+ # If any enumerable in other_enums is smaller than self, fills to `self.size`
1969
+ # with `nil`:
1970
+ #
1971
+ # a = [:a0, :a1, :a2, :a3]
1972
+ # b = [:b0, :b1, :b2]
1973
+ # c = [:c0, :c1]
1974
+ # d = a.zip(b, c)
1975
+ # d # => [[:a0, :b0, :c0], [:a1, :b1, :c1], [:a2, :b2, nil], [:a3, nil, nil]]
1976
+ #
1977
+ # If any enumerable in other_enums is larger than self, its trailing elements
1978
+ # are ignored:
1979
+ #
1980
+ # a = [:a0, :a1, :a2, :a3]
1981
+ # b = [:b0, :b1, :b2, :b3, :b4]
1982
+ # c = [:c0, :c1, :c2, :c3, :c4, :c5]
1983
+ # d = a.zip(b, c)
1984
+ # d # => [[:a0, :b0, :c0], [:a1, :b1, :c1], [:a2, :b2, :c2], [:a3, :b3, :c3]]
1985
+ #
1986
+ # When a block is given, calls the block with each of the sub-arrays (formed as
1987
+ # above); returns nil:
1988
+ #
1989
+ # a = [:a0, :a1, :a2, :a3]
1990
+ # b = [:b0, :b1, :b2, :b3]
1991
+ # c = [:c0, :c1, :c2, :c3]
1992
+ # a.zip(b, c) {|sub_array| p sub_array} # => nil
1993
+ #
1994
+ # Output:
1995
+ #
1996
+ # [:a0, :b0, :c0]
1997
+ # [:a1, :b1, :c1]
1998
+ # [:a2, :b2, :c2]
1999
+ # [:a3, :b3, :c3]
2000
+ #
2001
+ def zip: [Elem2] (::Enumerable[Elem2] enum) -> ::Array[[ Elem, Elem2 | nil ]]
2002
+ | [U, Elem2] (::Enumerable[Elem2]) { ([ Elem, Elem2 | nil ]) -> U } -> nil
413
2003
 
414
- def chunk: [U] () { (Elem elt) -> U } -> ::Enumerator[[U, ::Array[Elem]], void]
415
- | () -> ::Enumerator[Elem, ::Enumerator[[untyped, ::Array[Elem]], void]]
2004
+ # <!--
2005
+ # rdoc-file=enum.c
2006
+ # - chunk {|array| ... } -> enumerator
2007
+ # -->
2008
+ # Each element in the returned enumerator is a 2-element array consisting of:
2009
+ #
2010
+ # * A value returned by the block.
2011
+ # * An array ("chunk") containing the element for which that value was
2012
+ # returned, and all following elements for which the block returned the same
2013
+ # value:
2014
+ #
2015
+ #
2016
+ # So that:
2017
+ #
2018
+ # * Each block return value that is different from its predecessor begins a
2019
+ # new chunk.
2020
+ # * Each block return value that is the same as its predecessor continues the
2021
+ # same chunk.
2022
+ #
2023
+ #
2024
+ # Example:
2025
+ #
2026
+ # e = (0..10).chunk {|i| (i / 3).floor } # => #<Enumerator: ...>
2027
+ # # The enumerator elements.
2028
+ # e.next # => [0, [0, 1, 2]]
2029
+ # e.next # => [1, [3, 4, 5]]
2030
+ # e.next # => [2, [6, 7, 8]]
2031
+ # e.next # => [3, [9, 10]]
2032
+ #
2033
+ # Method `chunk` is especially useful for an enumerable that is already sorted.
2034
+ # This example counts words for each initial letter in a large array of words:
2035
+ #
2036
+ # # Get sorted words from a web page.
2037
+ # url = 'https://raw.githubusercontent.com/eneko/data-repository/master/data/words.txt'
2038
+ # words = URI::open(url).readlines
2039
+ # # Make chunks, one for each letter.
2040
+ # e = words.chunk {|word| word.upcase[0] } # => #<Enumerator: ...>
2041
+ # # Display 'A' through 'F'.
2042
+ # e.each {|c, words| p [c, words.length]; break if c == 'F' }
2043
+ #
2044
+ # Output:
2045
+ #
2046
+ # ["A", 17096]
2047
+ # ["B", 11070]
2048
+ # ["C", 19901]
2049
+ # ["D", 10896]
2050
+ # ["E", 8736]
2051
+ # ["F", 6860]
2052
+ #
2053
+ # You can use the special symbol `:_alone` to force an element into its own
2054
+ # separate chuck:
2055
+ #
2056
+ # a = [0, 0, 1, 1]
2057
+ # e = a.chunk{|i| i.even? ? :_alone : true }
2058
+ # e.to_a # => [[:_alone, [0]], [:_alone, [0]], [true, [1, 1]]]
2059
+ #
2060
+ # For example, you can put each line that contains a URL into its own chunk:
2061
+ #
2062
+ # pattern = /http/
2063
+ # open(filename) { |f|
2064
+ # f.chunk { |line| line =~ pattern ? :_alone : true }.each { |key, lines|
2065
+ # pp lines
2066
+ # }
2067
+ # }
2068
+ #
2069
+ # You can use the special symbol `:_separator` or `nil` to force an element to
2070
+ # be ignored (not included in any chunk):
2071
+ #
2072
+ # a = [0, 0, -1, 1, 1]
2073
+ # e = a.chunk{|i| i < 0 ? :_separator : true }
2074
+ # e.to_a # => [[true, [0, 0]], [true, [1, 1]]]
2075
+ #
2076
+ # Note that the separator does end the chunk:
2077
+ #
2078
+ # a = [0, 0, -1, 1, -1, 1]
2079
+ # e = a.chunk{|i| i < 0 ? :_separator : true }
2080
+ # e.to_a # => [[true, [0, 0]], [true, [1]], [true, [1]]]
2081
+ #
2082
+ # For example, the sequence of hyphens in svn log can be eliminated as follows:
2083
+ #
2084
+ # sep = "-"*72 + "\n"
2085
+ # IO.popen("svn log README") { |f|
2086
+ # f.chunk { |line|
2087
+ # line != sep || nil
2088
+ # }.each { |_, lines|
2089
+ # pp lines
2090
+ # }
2091
+ # }
2092
+ # #=> ["r20018 | knu | 2008-10-29 13:20:42 +0900 (Wed, 29 Oct 2008) | 2 lines\n",
2093
+ # # "\n",
2094
+ # # "* README, README.ja: Update the portability section.\n",
2095
+ # # "\n"]
2096
+ # # ["r16725 | knu | 2008-05-31 23:34:23 +0900 (Sat, 31 May 2008) | 2 lines\n",
2097
+ # # "\n",
2098
+ # # "* README, README.ja: Add a note about default C flags.\n",
2099
+ # # "\n"]
2100
+ # # ...
2101
+ #
2102
+ # Paragraphs separated by empty lines can be parsed as follows:
2103
+ #
2104
+ # File.foreach("README").chunk { |line|
2105
+ # /\A\s*\z/ !~ line || nil
2106
+ # }.each { |_, lines|
2107
+ # pp lines
2108
+ # }
2109
+ #
2110
+ def chunk: [U] () { (Elem elt) -> U } -> ::Enumerator[[ U, ::Array[Elem] ], void]
2111
+ | () -> ::Enumerator[Elem, ::Enumerator[[ untyped, ::Array[Elem] ], void]]
416
2112
 
2113
+ # <!--
2114
+ # rdoc-file=enum.c
2115
+ # - chunk_while {|element, next_element| ... } -> enumerator
2116
+ # -->
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:
2120
+ #
2121
+ # Example:
2122
+ #
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 }
2126
+ #
2127
+ # Output:
2128
+ #
2129
+ # [1, 2]
2130
+ # [4]
2131
+ # [9, 10, 11, 12]
2132
+ # [15, 16]
2133
+ # [19, 20, 21]
2134
+ #
417
2135
  def chunk_while: () { (Elem elt_before, Elem elt_after) -> boolish } -> ::Enumerator[::Array[Elem], void]
418
2136
 
2137
+ # <!--
2138
+ # rdoc-file=enum.c
2139
+ # - slice_when {|element, next_element| ... } -> enumerator
2140
+ # -->
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:
2144
+ #
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 }
2148
+ #
2149
+ # Output:
2150
+ #
2151
+ # [0, 1, 2]
2152
+ # [4, 5, 6]
2153
+ # [8, 9]
2154
+ #
419
2155
  def slice_when: () { (Elem elt_before, Elem elt_after) -> boolish } -> ::Enumerator[::Array[Elem], void]
420
2156
 
2157
+ # <!--
2158
+ # rdoc-file=enum.c
2159
+ # - slice_after(pattern) -> enumerator
2160
+ # - slice_after {|array| ... } -> enumerator
2161
+ # -->
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 }
2169
+ #
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:
2180
+ #
2181
+ # e = (1..20).slice_after {|i| i % 4 == 2 } # => #<Enumerator: ...>
2182
+ # e.each {|array| p array }
2183
+ #
2184
+ # Output:
2185
+ #
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]
2192
+ #
2193
+ # Other methods of the Enumerator class and Enumerable module, such as `map`,
2194
+ # etc., are also usable.
2195
+ #
2196
+ # For example, continuation lines (lines end with backslash) can be concatenated
2197
+ # as follows:
2198
+ #
2199
+ # lines = ["foo\n", "bar\\\n", "baz\n", "\n", "qux\n"]
2200
+ # e = lines.slice_after(/(?<!\\)\n\z/)
2201
+ # p e.to_a
2202
+ # #=> [["foo\n"], ["bar\\\n", "baz\n"], ["\n"], ["qux\n"]]
2203
+ # p e.map {|ll| ll[0...-1].map {|l| l.sub(/\\\n\z/, "") }.join + ll.last }
2204
+ # #=>["foo\n", "barbaz\n", "\n", "qux\n"]
2205
+ #
421
2206
  def slice_after: (untyped pattern) -> ::Enumerator[::Array[Elem], void]
422
2207
  | () { (Elem elt) -> boolish } -> ::Enumerator[::Array[Elem], void]
423
2208
 
2209
+ # <!--
2210
+ # rdoc-file=enum.c
2211
+ # - slice_before(pattern) -> enumerator
2212
+ # - slice_before {|array| ... } -> enumerator
2213
+ # -->
2214
+ # With argument `pattern`, returns an enumerator that uses the pattern to
2215
+ # partition elements into arrays ("slices"). An element begins a new slice if
2216
+ # `element === pattern` (or if it is the first element).
2217
+ #
2218
+ # a = %w[foo bar fop for baz fob fog bam foy]
2219
+ # e = a.slice_before(/ba/) # => #<Enumerator: ...>
2220
+ # e.each {|array| p array }
2221
+ #
2222
+ # Output:
2223
+ #
2224
+ # ["foo"]
2225
+ # ["bar", "fop", "for"]
2226
+ # ["baz", "fob", "fog"]
2227
+ # ["bam", "foy"]
2228
+ #
2229
+ # With a block, returns an enumerator that uses the block to partition elements
2230
+ # into arrays. An element begins a new slice if its block return is a truthy
2231
+ # value (or if it is the first element):
2232
+ #
2233
+ # e = (1..20).slice_before {|i| i % 4 == 2 } # => #<Enumerator: ...>
2234
+ # e.each {|array| p array }
2235
+ #
2236
+ # Output:
2237
+ #
2238
+ # [1]
2239
+ # [2, 3, 4, 5]
2240
+ # [6, 7, 8, 9]
2241
+ # [10, 11, 12, 13]
2242
+ # [14, 15, 16, 17]
2243
+ # [18, 19, 20]
2244
+ #
2245
+ # Other methods of the Enumerator class and Enumerable module, such as `to_a`,
2246
+ # `map`, etc., are also usable.
2247
+ #
2248
+ # For example, iteration over ChangeLog entries can be implemented as follows:
2249
+ #
2250
+ # # iterate over ChangeLog entries.
2251
+ # open("ChangeLog") { |f|
2252
+ # f.slice_before(/\A\S/).each { |e| pp e }
2253
+ # }
2254
+ #
2255
+ # # same as above. block is used instead of pattern argument.
2256
+ # open("ChangeLog") { |f|
2257
+ # f.slice_before { |line| /\A\S/ === line }.each { |e| pp e }
2258
+ # }
2259
+ #
2260
+ # "svn proplist -R" produces multiline output for each file. They can be chunked
2261
+ # as follows:
2262
+ #
2263
+ # IO.popen([{"LC_ALL"=>"C"}, "svn", "proplist", "-R"]) { |f|
2264
+ # f.lines.slice_before(/\AProp/).each { |lines| p lines }
2265
+ # }
2266
+ # #=> ["Properties on '.':\n", " svn:ignore\n", " svk:merge\n"]
2267
+ # # ["Properties on 'goruby.c':\n", " svn:eol-style\n"]
2268
+ # # ["Properties on 'complex.c':\n", " svn:mime-type\n", " svn:eol-style\n"]
2269
+ # # ["Properties on 'regparse.c':\n", " svn:eol-style\n"]
2270
+ # # ...
2271
+ #
2272
+ # If the block needs to maintain state over multiple elements, local variables
2273
+ # can be used. For example, three or more consecutive increasing numbers can be
2274
+ # squashed as follows (see `chunk_while` for a better way):
2275
+ #
2276
+ # a = [0, 2, 3, 4, 6, 7, 9]
2277
+ # prev = a[0]
2278
+ # p a.slice_before { |e|
2279
+ # prev, prev2 = e, prev
2280
+ # prev2 + 1 != e
2281
+ # }.map { |es|
2282
+ # es.length <= 2 ? es.join(",") : "#{es.first}-#{es.last}"
2283
+ # }.join(",")
2284
+ # #=> "0,2-4,6,7,9"
2285
+ #
2286
+ # However local variables should be used carefully if the result enumerator is
2287
+ # enumerated twice or more. The local variables should be initialized for each
2288
+ # enumeration. Enumerator.new can be used to do it.
2289
+ #
2290
+ # # Word wrapping. This assumes all characters have same width.
2291
+ # def wordwrap(words, maxwidth)
2292
+ # Enumerator.new {|y|
2293
+ # # cols is initialized in Enumerator.new.
2294
+ # cols = 0
2295
+ # words.slice_before { |w|
2296
+ # cols += 1 if cols != 0
2297
+ # cols += w.length
2298
+ # if maxwidth < cols
2299
+ # cols = w.length
2300
+ # true
2301
+ # else
2302
+ # false
2303
+ # end
2304
+ # }.each {|ws| y.yield ws }
2305
+ # }
2306
+ # end
2307
+ # text = (1..20).to_a.join(" ")
2308
+ # enum = wordwrap(text.split(/\s+/), 10)
2309
+ # puts "-"*10
2310
+ # enum.each { |ws| puts ws.join(" ") } # first enumeration.
2311
+ # puts "-"*10
2312
+ # enum.each { |ws| puts ws.join(" ") } # second enumeration generates same result as the first.
2313
+ # puts "-"*10
2314
+ # #=> ----------
2315
+ # # 1 2 3 4 5
2316
+ # # 6 7 8 9 10
2317
+ # # 11 12 13
2318
+ # # 14 15 16
2319
+ # # 17 18 19
2320
+ # # 20
2321
+ # # ----------
2322
+ # # 1 2 3 4 5
2323
+ # # 6 7 8 9 10
2324
+ # # 11 12 13
2325
+ # # 14 15 16
2326
+ # # 17 18 19
2327
+ # # 20
2328
+ # # ----------
2329
+ #
2330
+ # mbox contains series of mails which start with Unix From line. So each mail
2331
+ # can be extracted by slice before Unix From line.
2332
+ #
2333
+ # # parse mbox
2334
+ # open("mbox") { |f|
2335
+ # f.slice_before { |line|
2336
+ # line.start_with? "From "
2337
+ # }.each { |mail|
2338
+ # unix_from = mail.shift
2339
+ # i = mail.index("\n")
2340
+ # header = mail[0...i]
2341
+ # body = mail[(i+1)..-1]
2342
+ # body.pop if body.last == "\n"
2343
+ # fields = header.slice_before { |line| !" \t".include?(line[0]) }.to_a
2344
+ # p unix_from
2345
+ # pp fields
2346
+ # pp body
2347
+ # }
2348
+ # }
2349
+ #
2350
+ # # split mails in mbox (slice before Unix From line after an empty line)
2351
+ # open("mbox") { |f|
2352
+ # emp = true
2353
+ # f.slice_before { |line|
2354
+ # prevemp = emp
2355
+ # emp = line == "\n"
2356
+ # prevemp && line.start_with?("From ")
2357
+ # }.each { |mail|
2358
+ # mail.pop if mail.last == "\n"
2359
+ # pp mail
2360
+ # }
2361
+ # }
2362
+ #
424
2363
  def slice_before: (untyped pattern) -> ::Enumerator[::Array[Elem], void]
425
2364
  | () { (Elem elt) -> boolish } -> ::Enumerator[::Array[Elem], void]
426
2365
  end