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/array.rbs CHANGED
@@ -1,21 +1,54 @@
1
- # Arrays are ordered, integer-indexed collections of any object.
1
+ # <!-- rdoc-file=array.c -->
2
+ # An Array is an ordered, integer-indexed collection of objects, called
3
+ # *elements*. Any object may be an Array element.
4
+ #
5
+ # ## Array Indexes
6
+ #
7
+ # Array indexing starts at 0, as in C or Java.
8
+ #
9
+ # A positive index is an offset from the first element:
10
+ # * Index 0 indicates the first element.
11
+ # * Index 1 indicates the second element.
12
+ # * ...
13
+ #
14
+ #
15
+ # A negative index is an offset, backwards, from the end of the array:
16
+ # * Index -1 indicates the last element.
17
+ # * Index -2 indicates the next-to-last element.
18
+ # * ...
19
+ #
20
+ #
21
+ # A non-negative index is *in range* if it is smaller than the size of the
22
+ # array. For a 3-element array:
23
+ # * Indexes 0 through 2 are in range.
24
+ # * Index 3 is out of range.
25
+ #
26
+ #
27
+ # A negative index is *in range* if its absolute value is not larger than the
28
+ # size of the array. For a 3-element array:
29
+ # * Indexes -1 through -3 are in range.
30
+ # * Index -4 is out of range.
2
31
  #
3
- # Array indexing starts at 0, as in C or Java. A negative index is assumed to
4
- # be relative to the end of the array---that is, an index of -1 indicates the
5
- # last element of the array, -2 is the next to last element in the array, and so
6
- # on.
7
32
  #
8
33
  # ## Creating Arrays
9
34
  #
10
- # A new array can be created by using the literal constructor `[]`. Arrays can
11
- # contain different types of objects. For example, the array below contains an
12
- # Integer, a String and a Float:
35
+ # You can create an Array object explicitly with:
36
+ #
37
+ # * An [array literal](doc/syntax/literals_rdoc.html#label-Array+Literals).
38
+ #
39
+ #
40
+ # You can convert certain objects to Arrays with:
41
+ #
42
+ # * Method [Array](Kernel.html#method-i-Array).
43
+ #
44
+ #
45
+ # An Array can contain different types of objects. For example, the array below
46
+ # contains an Integer, a String and a Float:
13
47
  #
14
48
  # ary = [1, "two", 3.0] #=> [1, "two", 3.0]
15
49
  #
16
- # An array can also be created by explicitly calling Array.new with zero, one
17
- # (the initial size of the Array) or two arguments (the initial size and a
18
- # default object).
50
+ # An array can also be created by calling Array.new with zero, one (the initial
51
+ # size of the Array) or two arguments (the initial size and a default object).
19
52
  #
20
53
  # ary = Array.new #=> []
21
54
  # Array.new(3) #=> [nil, nil, nil]
@@ -231,64 +264,356 @@
231
264
  # arr = [1, 2, 3, 4, 5, 6]
232
265
  # arr.keep_if {|a| a < 4} #=> [1, 2, 3]
233
266
  # arr #=> [1, 2, 3]
234
- # for pack.c
235
267
  #
268
+ # ## What's Here
269
+ #
270
+ # First, what's elsewhere. Class Array:
271
+ #
272
+ # * Inherits from [class
273
+ # Object](Object.html#class-Object-label-What-27s+Here).
274
+ # * Includes [module
275
+ # Enumerable](Enumerable.html#module-Enumerable-label-What-27s+Here), which
276
+ # provides dozens of additional methods.
277
+ #
278
+ #
279
+ # Here, class Array provides methods that are useful for:
280
+ #
281
+ # * [Creating an Array](#class-Array-label-Methods+for+Creating+an+Array)
282
+ # * [Querying](#class-Array-label-Methods+for+Querying)
283
+ # * [Comparing](#class-Array-label-Methods+for+Comparing)
284
+ # * [Fetching](#class-Array-label-Methods+for+Fetching)
285
+ # * [Assigning](#class-Array-label-Methods+for+Assigning)
286
+ # * [Deleting](#class-Array-label-Methods+for+Deleting)
287
+ # * [Combining](#class-Array-label-Methods+for+Combining)
288
+ # * [Iterating](#class-Array-label-Methods+for+Iterating)
289
+ # * [Converting](#class-Array-label-Methods+for+Converting)
290
+ # * [And more....](#class-Array-label-Other+Methods)
291
+ #
292
+ #
293
+ # ### Methods for Creating an Array
294
+ #
295
+ # ::[]
296
+ # : Returns a new array populated with given objects.
297
+ # ::new
298
+ # : Returns a new array.
299
+ # ::try_convert
300
+ # : Returns a new array created from a given object.
301
+ #
302
+ #
303
+ # ### Methods for Querying
304
+ #
305
+ # #length, #size
306
+ # : Returns the count of elements.
307
+ # #include?
308
+ # : Returns whether any element `==` a given object.
309
+ # #empty?
310
+ # : Returns whether there are no elements.
311
+ # #all?
312
+ # : Returns whether all elements meet a given criterion.
313
+ # #any?
314
+ # : Returns whether any element meets a given criterion.
315
+ # #none?
316
+ # : Returns whether no element `==` a given object.
317
+ # #one?
318
+ # : Returns whether exactly one element `==` a given object.
319
+ # #count
320
+ # : Returns the count of elements that meet a given criterion.
321
+ # #find_index, #index
322
+ # : Returns the index of the first element that meets a given criterion.
323
+ # #rindex
324
+ # : Returns the index of the last element that meets a given criterion.
325
+ # #hash
326
+ # : Returns the integer hash code.
327
+ #
328
+ #
329
+ # ### Methods for Comparing
330
+ # [#<=>](#method-i-3C-3D-3E)
331
+ # : Returns -1, 0, or 1 as `self` is less than, equal to, or greater than a
332
+ # given object.
333
+ # [#==](#method-i-3D-3D)
334
+ # : Returns whether each element in `self` is `==` to the corresponding
335
+ # element in a given object.
336
+ # #eql?
337
+ # : Returns whether each element in `self` is `eql?` to the corresponding
338
+ # element in a given object.
339
+ #
340
+ #
341
+ # ### Methods for Fetching
342
+ #
343
+ # These methods do not modify `self`.
344
+ #
345
+ # #[]
346
+ # : Returns one or more elements.
347
+ # #fetch
348
+ # : Returns the element at a given offset.
349
+ # #first
350
+ # : Returns one or more leading elements.
351
+ # #last
352
+ # : Returns one or more trailing elements.
353
+ # #max
354
+ # : Returns one or more maximum-valued elements, as determined by `<=>` or a
355
+ # given block.
356
+ # #max
357
+ # : Returns one or more minimum-valued elements, as determined by `<=>` or a
358
+ # given block.
359
+ # #minmax
360
+ # : Returns the minimum-valued and maximum-valued elements, as determined by
361
+ # `<=>` or a given block.
362
+ # #assoc
363
+ # : Returns the first element that is an array whose first element `==` a
364
+ # given object.
365
+ # #rassoc
366
+ # : Returns the first element that is an array whose second element `==` a
367
+ # given object.
368
+ # #at
369
+ # : Returns the element at a given offset.
370
+ # #values_at
371
+ # : Returns the elements at given offsets.
372
+ # #dig
373
+ # : Returns the object in nested objects that is specified by a given index
374
+ # and additional arguments.
375
+ # #drop
376
+ # : Returns trailing elements as determined by a given index.
377
+ # #take
378
+ # : Returns leading elements as determined by a given index.
379
+ # #drop_while
380
+ # : Returns trailing elements as determined by a given block.
381
+ # #take_while
382
+ # : Returns leading elements as determined by a given block.
383
+ # #slice
384
+ # : Returns consecutive elements as determined by a given argument.
385
+ # #sort
386
+ # : Returns all elements in an order determined by `<=>` or a given block.
387
+ # #reverse
388
+ # : Returns all elements in reverse order.
389
+ # #compact
390
+ # : Returns an array containing all non-`nil` elements.
391
+ # #select, #filter
392
+ # : Returns an array containing elements selected by a given block.
393
+ # #uniq
394
+ # : Returns an array containing non-duplicate elements.
395
+ # #rotate
396
+ # : Returns all elements with some rotated from one end to the other.
397
+ # #bsearch
398
+ # : Returns an element selected via a binary search as determined by a given
399
+ # block.
400
+ # #bsearch_index
401
+ # : Returns the index of an element selected via a binary search as determined
402
+ # by a given block.
403
+ # #sample
404
+ # : Returns one or more random elements.
405
+ # #shuffle
406
+ # : Returns elements in a random order.
407
+ #
408
+ #
409
+ # ### Methods for Assigning
410
+ #
411
+ # These methods add, replace, or reorder elements in `self`.
412
+ #
413
+ # #[]=
414
+ # : Assigns specified elements with a given object.
415
+ # #push, #append, #<<
416
+ # : Appends trailing elements.
417
+ # #unshift, #prepend
418
+ # : Prepends leading elements.
419
+ # #insert
420
+ # : Inserts given objects at a given offset; does not replace elements.
421
+ # #concat
422
+ # : Appends all elements from given arrays.
423
+ # #fill
424
+ # : Replaces specified elements with specified objects.
425
+ # #replace
426
+ # : Replaces the content of `self` with the content of a given array.
427
+ # #reverse!
428
+ # : Replaces `self` with its elements reversed.
429
+ # #rotate!
430
+ # : Replaces `self` with its elements rotated.
431
+ # #shuffle!
432
+ # : Replaces `self` with its elements in random order.
433
+ # #sort!
434
+ # : Replaces `self` with its elements sorted, as determined by `<=>` or a
435
+ # given block.
436
+ # #sort_by!
437
+ # : Replaces `self` with its elements sorted, as determined by a given block.
438
+ #
439
+ #
440
+ # ### Methods for Deleting
441
+ #
442
+ # Each of these methods removes elements from `self`:
443
+ #
444
+ # #pop
445
+ # : Removes and returns the last element.
446
+ # #shift
447
+ # : Removes and returns the first element.
448
+ # #compact!
449
+ # : Removes all non-`nil` elements.
450
+ # #delete
451
+ # : Removes elements equal to a given object.
452
+ # #delete_at
453
+ # : Removes the element at a given offset.
454
+ # #delete_if
455
+ # : Removes elements specified by a given block.
456
+ # #keep_if
457
+ # : Removes elements not specified by a given block.
458
+ # #reject!
459
+ # : Removes elements specified by a given block.
460
+ # #select!, #filter!
461
+ # : Removes elements not specified by a given block.
462
+ # #slice!
463
+ # : Removes and returns a sequence of elements.
464
+ # #uniq!
465
+ # : Removes duplicates.
466
+ #
467
+ #
468
+ # ### Methods for Combining
469
+ #
470
+ # [#&](#method-i-26)
471
+ # : Returns an array containing elements found both in `self` and a given
472
+ # array.
473
+ # #intersection
474
+ # : Returns an array containing elements found both in `self` and in each
475
+ # given array.
476
+ # #+
477
+ # : Returns an array containing all elements of `self` followed by all
478
+ # elements of a given array.
479
+ # #-
480
+ # : Returns an array containiing all elements of `self` that are not found in
481
+ # a given array.
482
+ # [#|](#method-i-7C)
483
+ # : Returns an array containing all elements of `self` and all elements of a
484
+ # given array, duplicates removed.
485
+ # #union
486
+ # : Returns an array containing all elements of `self` and all elements of
487
+ # given arrays, duplicates removed.
488
+ # #difference
489
+ # : Returns an array containing all elements of `self` that are not found in
490
+ # any of the given arrays..
491
+ # #product
492
+ # : Returns or yields all combinations of elements from `self` and given
493
+ # arrays.
494
+ #
495
+ #
496
+ # ### Methods for Iterating
497
+ #
498
+ # #each
499
+ # : Passes each element to a given block.
500
+ # #reverse_each
501
+ # : Passes each element, in reverse order, to a given block.
502
+ # #each_index
503
+ # : Passes each element index to a given block.
504
+ # #cycle
505
+ # : Calls a given block with each element, then does so again, for a specified
506
+ # number of times, or forever.
507
+ # #combination
508
+ # : Calls a given block with combinations of elements of `self`; a combination
509
+ # does not use the same element more than once.
510
+ # #permutation
511
+ # : Calls a given block with permutations of elements of `self`; a permutation
512
+ # does not use the same element more than once.
513
+ # #repeated_combination
514
+ # : Calls a given block with combinations of elements of `self`; a combination
515
+ # may use the same element more than once.
516
+ # #repeated_permutation
517
+ # : Calls a given block with permutations of elements of `self`; a permutation
518
+ # may use the same element more than once.
519
+ #
520
+ #
521
+ # ### Methods for Converting
522
+ #
523
+ # #map, #collect
524
+ # : Returns an array containing the block return-value for each element.
525
+ # #map!, #collect!
526
+ # : Replaces each element with a block return-value.
527
+ # #flatten
528
+ # : Returns an array that is a recursive flattening of `self`.
529
+ # #flatten!
530
+ # : Replaces each nested array in `self` with the elements from that array.
531
+ # #inspect, #to_s
532
+ # : Returns a new String containing the elements.
533
+ # #join
534
+ # : Returns a newsString containing the elements joined by the field
535
+ # separator.
536
+ # #to_a
537
+ # : Returns `self` or a new array containing all elements.
538
+ # #to_ary
539
+ # : Returns `self`.
540
+ # #to_h
541
+ # : Returns a new hash formed from the elements.
542
+ # #transpose
543
+ # : Transposes `self`, which must be an array of arrays.
544
+ # #zip
545
+ # : Returns a new array of arrays containing `self` and given arrays; follow
546
+ # the link for details.
547
+ #
548
+ #
549
+ # ### Other Methods
550
+ #
551
+ # #*
552
+ # : Returns one of the following:
553
+ # * With integer argument `n`, a new array that is the concatenation of
554
+ # `n` copies of `self`.
555
+ # * With string argument `field_separator`, a new string that is
556
+ # equivalent to `join(field_separator)`.
557
+ #
558
+ # #abbrev
559
+ # : Returns a hash of unambiguous abbreviations for elements.
560
+ # #pack
561
+ # : Packs the elements into a binary sequence.
562
+ # #sum
563
+ # : Returns a sum of elements according to either `+` or a given block.
564
+ #
565
+ %a{annotate:rdoc:source:from=array.c}
236
566
  class Array[unchecked out Elem] < Object
237
567
  include Enumerable[Elem]
238
568
 
239
- # Returns a new array.
240
- #
241
- # In the first form, if no arguments are sent, the new array will be empty. When
242
- # a `size` and an optional `default` are sent, an array is created with `size`
243
- # copies of `default`. Take notice that all elements will reference the same
244
- # object `default`.
245
- #
246
- # The second form creates a copy of the array passed as a parameter (the array
247
- # is generated by calling to_ary on the parameter).
248
- #
249
- # first_array = ["Matz", "Guido"]
250
- #
251
- # second_array = Array.new(first_array) #=> ["Matz", "Guido"]
252
- #
253
- # first_array.equal? second_array #=> false
254
- #
255
- # In the last form, an array of the given size is created. Each element in this
256
- # array is created by passing the element's index to the given block and storing
257
- # the return value.
258
- #
259
- # Array.new(3) {|index| index ** 2}
260
- # # => [0, 1, 4]
261
- #
262
- # ## Common gotchas
263
- #
264
- # When sending the second parameter, the same object will be used as the value
265
- # for all the array elements:
266
- #
267
- # a = Array.new(2, Hash.new)
268
- # # => [{}, {}]
269
- #
270
- # a[0]['cat'] = 'feline'
271
- # a # => [{"cat"=>"feline"}, {"cat"=>"feline"}]
272
- #
273
- # a[1]['cat'] = 'Felix'
274
- # a # => [{"cat"=>"Felix"}, {"cat"=>"Felix"}]
275
- #
276
- # Since all the Array elements store the same hash, changes to one of them will
277
- # affect them all.
278
- #
279
- # If multiple copies are what you want, you should use the block version which
280
- # uses the result of that block each time an element of the array needs to be
281
- # initialized:
282
- #
283
- # a = Array.new(2) {Hash.new}
284
- # a[0]['cat'] = 'feline'
285
- # a # => [{"cat"=>"feline"}, {}]
569
+ # <!--
570
+ # rdoc-file=array.c
571
+ # - Array.new -> new_empty_array
572
+ # - Array.new(array) -> new_array
573
+ # - Array.new(size) -> new_array
574
+ # - Array.new(size, default_value) -> new_array
575
+ # - Array.new(size) {|index| ... } -> new_array
576
+ # -->
577
+ # Returns a new Array.
578
+ #
579
+ # With no block and no arguments, returns a new empty Array object.
580
+ #
581
+ # With no block and a single Array argument `array`, returns a new Array formed
582
+ # from `array`:
583
+ # a = Array.new([:foo, 'bar', 2])
584
+ # a.class # => Array
585
+ # a # => [:foo, "bar", 2]
586
+ #
587
+ # With no block and a single Integer argument `size`, returns a new Array of the
588
+ # given size whose elements are all `nil`:
589
+ # a = Array.new(3)
590
+ # a # => [nil, nil, nil]
591
+ #
592
+ # With no block and arguments `size` and `default_value`, returns an Array of
593
+ # the given size; each element is that same `default_value`:
594
+ # a = Array.new(3, 'x')
595
+ # a # => ['x', 'x', 'x']
596
+ #
597
+ # With a block and argument `size`, returns an Array of the given size; the
598
+ # block is called with each successive integer `index`; the element for that
599
+ # `index` is the return value from the block:
600
+ # a = Array.new(3) {|index| "Element #{index}" }
601
+ # a # => ["Element 0", "Element 1", "Element 2"]
602
+ #
603
+ # Raises ArgumentError if `size` is negative.
604
+ #
605
+ # With a block and no argument, or a single argument `0`, ignores the block and
606
+ # returns a new empty Array.
286
607
  #
287
608
  def initialize: () -> void
288
609
  | (::Array[Elem] ary) -> void
289
610
  | (int size, ?Elem val) -> void
290
611
  | (int size) { (::Integer index) -> Elem } -> void
291
612
 
613
+ # <!--
614
+ # rdoc-file=array.c
615
+ # - [](*args)
616
+ # -->
292
617
  # Returns a new array populated with the given objects.
293
618
  #
294
619
  # Array.[]( 1, 'a', /^A/) # => [1, "a", /^A/]
@@ -297,190 +622,336 @@ class Array[unchecked out Elem] < Object
297
622
  #
298
623
  def self.[]: [U] (*U) -> ::Array[U]
299
624
 
300
- # Tries to convert `obj` into an array, using the `to_ary` method. Returns the
301
- # converted array or `nil` if `obj` cannot be converted. This method can be used
302
- # to check if an argument is an array.
625
+ # <!--
626
+ # rdoc-file=array.c
627
+ # - Array.try_convert(object) -> object, new_array, or nil
628
+ # -->
629
+ # If `object` is an Array object, returns `object`.
630
+ #
631
+ # Otherwise if `object` responds to `:to_ary`, calls `object.to_ary` and returns
632
+ # the result.
303
633
  #
304
- # Array.try_convert([1]) #=> [1]
305
- # Array.try_convert("1") #=> nil
634
+ # Returns `nil` if `object` does not respond to `:to_ary`
306
635
  #
307
- # if tmp = Array.try_convert(arg)
308
- # # the argument is an array
309
- # elsif tmp = String.try_convert(arg)
310
- # # the argument is a string
311
- # end
636
+ # Raises an exception unless `object.to_ary` returns an Array object.
312
637
  #
313
638
  def self.try_convert: [U] (untyped) -> ::Array[U]?
314
639
 
315
640
  public
316
641
 
317
- # Set Intersection --- Returns a new array containing unique elements common to
318
- # the two arrays. The order is preserved from the original array.
319
- #
320
- # It compares elements using their #hash and #eql? methods for efficiency.
642
+ # <!--
643
+ # rdoc-file=array.c
644
+ # - array & other_array -> new_array
645
+ # -->
646
+ # Returns a new Array containing each element found in both `array` and Array
647
+ # `other_array`; duplicates are omitted; items are compared using `eql?`:
648
+ # [0, 1, 2, 3] & [1, 2] # => [1, 2]
649
+ # [0, 1, 0, 1] & [0, 1] # => [0, 1]
321
650
  #
322
- # [ 1, 1, 3, 5 ] & [ 3, 2, 1 ] #=> [ 1, 3 ]
323
- # [ 'a', 'b', 'b', 'z' ] & [ 'a', 'b', 'c' ] #=> [ 'a', 'b' ]
651
+ # Preserves order from `array`:
652
+ # [0, 1, 2] & [3, 2, 1, 0] # => [0, 1, 2]
324
653
  #
325
- # See also Array#uniq.
654
+ # Related: Array#intersection.
326
655
  #
327
656
  def &: (::Array[untyped] | _ToAry[untyped]) -> ::Array[Elem]
328
657
 
329
- # Repetition --- With a String argument, equivalent to `ary.join(str)`.
330
- #
331
- # Otherwise, returns a new array built by concatenating the `int` copies of
332
- # `self`.
658
+ # <!--
659
+ # rdoc-file=array.c
660
+ # - array * n -> new_array
661
+ # - array * string_separator -> new_string
662
+ # -->
663
+ # When non-negative argument Integer `n` is given, returns a new Array built by
664
+ # concatenating the `n` copies of `self`:
665
+ # a = ['x', 'y']
666
+ # a * 3 # => ["x", "y", "x", "y", "x", "y"]
333
667
  #
334
- # [ 1, 2, 3 ] * 3 #=> [ 1, 2, 3, 1, 2, 3, 1, 2, 3 ]
335
- # [ 1, 2, 3 ] * "," #=> "1,2,3"
668
+ # When String argument `string_separator` is given, equivalent to
669
+ # `array.join(string_separator)`:
670
+ # [0, [0, 1], {foo: 0}] * ', ' # => "0, 0, 1, {:foo=>0}"
336
671
  #
337
672
  def *: (string str) -> ::String
338
673
  | (int int) -> ::Array[Elem]
339
674
 
340
- # Concatenation --- Returns a new array built by concatenating the two arrays
341
- # together to produce a third array.
342
- #
343
- # [ 1, 2, 3 ] + [ 4, 5 ] #=> [ 1, 2, 3, 4, 5 ]
344
- # a = [ "a", "b", "c" ]
345
- # c = a + [ "d", "e", "f" ]
346
- # c #=> [ "a", "b", "c", "d", "e", "f" ]
347
- # a #=> [ "a", "b", "c" ]
348
- #
349
- # Note that
350
- # x += y
351
- #
352
- # is the same as
353
- # x = x + y
675
+ # <!--
676
+ # rdoc-file=array.c
677
+ # - array + other_array -> new_array
678
+ # -->
679
+ # Returns a new Array containing all elements of `array` followed by all
680
+ # elements of `other_array`:
681
+ # a = [0, 1] + [2, 3]
682
+ # a # => [0, 1, 2, 3]
354
683
  #
355
- # This means that it produces a new array. As a consequence, repeated use of
356
- # `+=` on arrays can be quite inefficient.
357
- #
358
- # See also Array#concat.
684
+ # Related: #concat.
359
685
  #
360
686
  def +: [U] (_ToAry[U]) -> ::Array[Elem | U]
361
687
 
362
- # Array Difference
363
- #
364
- # Returns a new array that is a copy of the original array, removing all
365
- # occurrences of any item that also appear in `other_ary`. The order is
366
- # preserved from the original array.
367
- #
368
- # It compares elements using their #hash and #eql? methods for efficiency.
369
- #
370
- # [ 1, 1, 2, 2, 3, 3, 4, 5 ] - [ 1, 2, 4 ] #=> [ 3, 3, 5 ]
371
- #
372
- # Note that while 1 and 2 were only present once in the array argument, and were
373
- # present twice in the receiver array, all occurrences of each Integer are
374
- # removed in the returned array.
688
+ # <!--
689
+ # rdoc-file=array.c
690
+ # - array - other_array -> new_array
691
+ # -->
692
+ # Returns a new Array containing only those elements from `array` that are not
693
+ # found in Array `other_array`; items are compared using `eql?`; the order from
694
+ # `array` is preserved:
695
+ # [0, 1, 1, 2, 1, 1, 3, 1, 1] - [1] # => [0, 2, 3]
696
+ # [0, 1, 2, 3] - [3, 0] # => [1, 2]
697
+ # [0, 1, 2] - [4] # => [0, 1, 2]
375
698
  #
376
- # If you need set-like behavior, see the library class Set.
377
- #
378
- # See also Array#difference.
699
+ # Related: Array#difference.
379
700
  #
380
701
  def -: (_ToAry[untyped]) -> ::Array[Elem]
381
702
 
382
- # Append---Pushes the given object on to the end of this array. This expression
383
- # returns the array itself, so several appends may be chained together.
703
+ # <!--
704
+ # rdoc-file=array.c
705
+ # - array << object -> self
706
+ # -->
707
+ # Appends `object` to `self`; returns `self`:
708
+ # a = [:foo, 'bar', 2]
709
+ # a << :baz # => [:foo, "bar", 2, :baz]
384
710
  #
385
- # a = [ 1, 2 ]
386
- # a << "c" << "d" << [ 3, 4 ]
387
- # #=> [ 1, 2, "c", "d", [ 3, 4 ] ]
388
- # a
389
- # #=> [ 1, 2, "c", "d", [ 3, 4 ] ]
711
+ # Appends `object` as one element, even if it is another Array:
712
+ # a = [:foo, 'bar', 2]
713
+ # a1 = a << [3, 4]
714
+ # a1 # => [:foo, "bar", 2, [3, 4]]
390
715
  #
391
716
  def <<: (Elem) -> self
392
717
 
393
- # Comparison --- Returns an integer (`-1`, `0`, or `+1`) if this array is less
394
- # than, equal to, or greater than `other_ary`.
718
+ # <!--
719
+ # rdoc-file=array.c
720
+ # - array <=> other_array -> -1, 0, or 1
721
+ # -->
722
+ # Returns -1, 0, or 1 as `self` is less than, equal to, or greater than
723
+ # `other_array`. For each index `i` in `self`, evaluates `result = self[i] <=>
724
+ # other_array[i]`.
395
725
  #
396
- # Each object in each array is compared (using the <=> operator).
726
+ # Returns -1 if any result is -1:
727
+ # [0, 1, 2] <=> [0, 1, 3] # => -1
397
728
  #
398
- # Arrays are compared in an "element-wise" manner; the first element of `ary` is
399
- # compared with the first one of `other_ary` using the <=> operator, then each
400
- # of the second elements, etc... As soon as the result of any such comparison is
401
- # non zero (i.e. the two corresponding elements are not equal), that result is
402
- # returned for the whole array comparison.
729
+ # Returns 1 if any result is 1:
730
+ # [0, 1, 2] <=> [0, 1, 1] # => 1
403
731
  #
404
- # If all the elements are equal, then the result is based on a comparison of the
405
- # array lengths. Thus, two arrays are "equal" according to Array#<=> if, and
406
- # only if, they have the same length and the value of each element is equal to
407
- # the value of the corresponding element in the other array.
732
+ # When all results are zero:
733
+ # * Returns -1 if `array` is smaller than `other_array`:
734
+ # [0, 1, 2] <=> [0, 1, 2, 3] # => -1
408
735
  #
409
- # `nil` is returned if the `other_ary` is not an array or if the comparison of
410
- # two elements returned `nil`.
736
+ # * Returns 1 if `array` is larger than `other_array`:
737
+ # [0, 1, 2] <=> [0, 1] # => 1
411
738
  #
412
- # [ "a", "a", "c" ] <=> [ "a", "b", "c" ] #=> -1
413
- # [ 1, 2, 3, 4, 5, 6 ] <=> [ 1, 2 ] #=> +1
414
- # [ 1, 2 ] <=> [ 1, :two ] #=> nil
739
+ # * Returns 0 if `array` and `other_array` are the same size:
740
+ # [0, 1, 2] <=> [0, 1, 2] # => 0
415
741
  #
416
742
  def <=>: (untyped) -> ::Integer?
417
743
 
418
- # Equality --- Two arrays are equal if they contain the same number of elements
419
- # and if each element is equal to (according to Object#==) the corresponding
420
- # element in `other_ary`.
744
+ # <!--
745
+ # rdoc-file=array.c
746
+ # - array == other_array -> true or false
747
+ # -->
748
+ # Returns `true` if both `array.size == other_array.size` and for each index `i`
749
+ # in `array`, `array[i] == other_array[i]`:
750
+ # a0 = [:foo, 'bar', 2]
751
+ # a1 = [:foo, 'bar', 2.0]
752
+ # a1 == a0 # => true
753
+ # [] == [] # => true
754
+ #
755
+ # Otherwise, returns `false`.
421
756
  #
422
- # [ "a", "c" ] == [ "a", "c", 7 ] #=> false
423
- # [ "a", "c", 7 ] == [ "a", "c", 7 ] #=> true
424
- # [ "a", "c", 7 ] == [ "a", "d", "f" ] #=> false
757
+ # This method is different from method Array#eql?, which compares elements using
758
+ # `Object#eql?`.
425
759
  #
426
760
  def ==: (untyped other) -> bool
427
761
 
428
- # Element Reference --- Returns the element at `index`, or returns a subarray
429
- # starting at the `start` index and continuing for `length` elements, or returns
430
- # a subarray specified by `range` of indices.
431
- #
432
- # Negative indices count backward from the end of the array (-1 is the last
433
- # element). For `start` and `range` cases the starting index is just before an
434
- # element. Additionally, an empty array is returned when the starting index for
435
- # an element range is at the end of the array.
436
- #
437
- # Returns `nil` if the index (or starting index) are out of range.
438
- #
439
- # a = [ "a", "b", "c", "d", "e" ]
440
- # a[2] + a[0] + a[1] #=> "cab"
441
- # a[6] #=> nil
442
- # a[1, 2] #=> [ "b", "c" ]
443
- # a[1..3] #=> [ "b", "c", "d" ]
444
- # a[4..7] #=> [ "e" ]
445
- # a[6..10] #=> nil
446
- # a[-3, 3] #=> [ "c", "d", "e" ]
447
- # # special cases
448
- # a[5] #=> nil
449
- # a[6, 1] #=> nil
450
- # a[5, 1] #=> []
451
- # a[5..10] #=> []
762
+ # <!--
763
+ # rdoc-file=array.c
764
+ # - array[index] -> object or nil
765
+ # - array[start, length] -> object or nil
766
+ # - array[range] -> object or nil
767
+ # - array[aseq] -> object or nil
768
+ # - array.slice(index) -> object or nil
769
+ # - array.slice(start, length) -> object or nil
770
+ # - array.slice(range) -> object or nil
771
+ # - array.slice(aseq) -> object or nil
772
+ # -->
773
+ # Returns elements from `self`; does not modify `self`.
774
+ #
775
+ # When a single Integer argument `index` is given, returns the element at offset
776
+ # `index`:
777
+ # a = [:foo, 'bar', 2]
778
+ # a[0] # => :foo
779
+ # a[2] # => 2
780
+ # a # => [:foo, "bar", 2]
781
+ #
782
+ # If `index` is negative, counts relative to the end of `self`:
783
+ # a = [:foo, 'bar', 2]
784
+ # a[-1] # => 2
785
+ # a[-2] # => "bar"
786
+ #
787
+ # If `index` is out of range, returns `nil`.
788
+ #
789
+ # When two Integer arguments `start` and `length` are given, returns a new Array
790
+ # of size `length` containing successive elements beginning at offset `start`:
791
+ # a = [:foo, 'bar', 2]
792
+ # a[0, 2] # => [:foo, "bar"]
793
+ # a[1, 2] # => ["bar", 2]
794
+ #
795
+ # If `start + length` is greater than `self.length`, returns all elements from
796
+ # offset `start` to the end:
797
+ # a = [:foo, 'bar', 2]
798
+ # a[0, 4] # => [:foo, "bar", 2]
799
+ # a[1, 3] # => ["bar", 2]
800
+ # a[2, 2] # => [2]
801
+ #
802
+ # If `start == self.size` and `length >= 0`, returns a new empty Array.
803
+ #
804
+ # If `length` is negative, returns `nil`.
805
+ #
806
+ # When a single Range argument `range` is given, treats `range.min` as `start`
807
+ # above and `range.size` as `length` above:
808
+ # a = [:foo, 'bar', 2]
809
+ # a[0..1] # => [:foo, "bar"]
810
+ # a[1..2] # => ["bar", 2]
811
+ #
812
+ # Special case: If `range.start == a.size`, returns a new empty Array.
813
+ #
814
+ # If `range.end` is negative, calculates the end index from the end:
815
+ # a = [:foo, 'bar', 2]
816
+ # a[0..-1] # => [:foo, "bar", 2]
817
+ # a[0..-2] # => [:foo, "bar"]
818
+ # a[0..-3] # => [:foo]
819
+ #
820
+ # If `range.start` is negative, calculates the start index from the end:
821
+ # a = [:foo, 'bar', 2]
822
+ # a[-1..2] # => [2]
823
+ # a[-2..2] # => ["bar", 2]
824
+ # a[-3..2] # => [:foo, "bar", 2]
825
+ #
826
+ # If `range.start` is larger than the array size, returns `nil`.
827
+ # a = [:foo, 'bar', 2]
828
+ # a[4..1] # => nil
829
+ # a[4..0] # => nil
830
+ # a[4..-1] # => nil
831
+ #
832
+ # When a single Enumerator::ArithmeticSequence argument `aseq` is given, returns
833
+ # an Array of elements corresponding to the indexes produced by the sequence.
834
+ # a = ['--', 'data1', '--', 'data2', '--', 'data3']
835
+ # a[(1..).step(2)] # => ["data1", "data2", "data3"]
836
+ #
837
+ # Unlike slicing with range, if the start or the end of the arithmetic sequence
838
+ # is larger than array size, throws RangeError.
839
+ # a = ['--', 'data1', '--', 'data2', '--', 'data3']
840
+ # a[(1..11).step(2)]
841
+ # # RangeError (((1..11).step(2)) out of range)
842
+ # a[(7..).step(2)]
843
+ # # RangeError (((7..).step(2)) out of range)
844
+ #
845
+ # If given a single argument, and its type is not one of the listed, tries to
846
+ # convert it to Integer, and raises if it is impossible:
847
+ # a = [:foo, 'bar', 2]
848
+ # # Raises TypeError (no implicit conversion of Symbol into Integer):
849
+ # a[:foo]
850
+ #
851
+ # Array#slice is an alias for Array#[].
452
852
  #
453
853
  def []: (int index) -> Elem
454
854
  | (int start, int length) -> ::Array[Elem]?
455
855
  | (::Range[::Integer?] range) -> ::Array[Elem]?
456
856
 
457
- # Element Assignment --- Sets the element at `index`, or replaces a subarray
458
- # from the `start` index for `length` elements, or replaces a subarray specified
459
- # by the `range` of indices.
460
- #
461
- # If indices are greater than the current capacity of the array, the array grows
462
- # automatically. Elements are inserted into the array at `start` if `length` is
463
- # zero.
464
- #
465
- # Negative indices will count backward from the end of the array. For `start`
466
- # and `range` cases the starting index is just before an element.
467
- #
468
- # An IndexError is raised if a negative index points past the beginning of the
469
- # array.
857
+ # <!--
858
+ # rdoc-file=array.c
859
+ # - array[index] = object -> object
860
+ # - array[start, length] = object -> object
861
+ # - array[range] = object -> object
862
+ # -->
863
+ # Assigns elements in `self`; returns the given `object`.
470
864
  #
471
- # See also Array#push, and Array#unshift.
865
+ # When Integer argument `index` is given, assigns `object` to an element in
866
+ # `self`.
472
867
  #
473
- # a = Array.new
474
- # a[4] = "4"; #=> [nil, nil, nil, nil, "4"]
475
- # a[0, 3] = [ 'a', 'b', 'c' ] #=> ["a", "b", "c", nil, "4"]
476
- # a[1..2] = [ 1, 2 ] #=> ["a", 1, 2, nil, "4"]
477
- # a[0, 2] = "?" #=> ["?", 2, nil, "4"]
478
- # a[0..2] = "A" #=> ["A", "4"]
479
- # a[-1] = "Z" #=> ["A", "Z"]
480
- # a[1..-1] = nil #=> ["A", nil]
481
- # a[1..-1] = [] #=> ["A"]
482
- # a[0, 0] = [ 1, 2 ] #=> [1, 2, "A"]
483
- # a[3, 0] = "B" #=> [1, 2, "A", "B"]
868
+ # If `index` is non-negative, assigns `object` the element at offset `index`:
869
+ # a = [:foo, 'bar', 2]
870
+ # a[0] = 'foo' # => "foo"
871
+ # a # => ["foo", "bar", 2]
872
+ #
873
+ # If `index` is greater than `self.length`, extends the array:
874
+ # a = [:foo, 'bar', 2]
875
+ # a[7] = 'foo' # => "foo"
876
+ # a # => [:foo, "bar", 2, nil, nil, nil, nil, "foo"]
877
+ #
878
+ # If `index` is negative, counts backwards from the end of the array:
879
+ # a = [:foo, 'bar', 2]
880
+ # a[-1] = 'two' # => "two"
881
+ # a # => [:foo, "bar", "two"]
882
+ #
883
+ # When Integer arguments `start` and `length` are given and `object` is not an
884
+ # Array, removes `length - 1` elements beginning at offset `start`, and assigns
885
+ # `object` at offset `start`:
886
+ # a = [:foo, 'bar', 2]
887
+ # a[0, 2] = 'foo' # => "foo"
888
+ # a # => ["foo", 2]
889
+ #
890
+ # If `start` is negative, counts backwards from the end of the array:
891
+ # a = [:foo, 'bar', 2]
892
+ # a[-2, 2] = 'foo' # => "foo"
893
+ # a # => [:foo, "foo"]
894
+ #
895
+ # If `start` is non-negative and outside the array (` >= self.size`), extends
896
+ # the array with `nil`, assigns `object` at offset `start`, and ignores
897
+ # `length`:
898
+ # a = [:foo, 'bar', 2]
899
+ # a[6, 50] = 'foo' # => "foo"
900
+ # a # => [:foo, "bar", 2, nil, nil, nil, "foo"]
901
+ #
902
+ # If `length` is zero, shifts elements at and following offset `start` and
903
+ # assigns `object` at offset `start`:
904
+ # a = [:foo, 'bar', 2]
905
+ # a[1, 0] = 'foo' # => "foo"
906
+ # a # => [:foo, "foo", "bar", 2]
907
+ #
908
+ # If `length` is too large for the existing array, does not extend the array:
909
+ # a = [:foo, 'bar', 2]
910
+ # a[1, 5] = 'foo' # => "foo"
911
+ # a # => [:foo, "foo"]
912
+ #
913
+ # When Range argument `range` is given and `object` is an Array, removes `length
914
+ # - 1` elements beginning at offset `start`, and assigns `object` at offset
915
+ # `start`:
916
+ # a = [:foo, 'bar', 2]
917
+ # a[0..1] = 'foo' # => "foo"
918
+ # a # => ["foo", 2]
919
+ #
920
+ # if `range.begin` is negative, counts backwards from the end of the array:
921
+ # a = [:foo, 'bar', 2]
922
+ # a[-2..2] = 'foo' # => "foo"
923
+ # a # => [:foo, "foo"]
924
+ #
925
+ # If the array length is less than `range.begin`, assigns `object` at offset
926
+ # `range.begin`, and ignores `length`:
927
+ # a = [:foo, 'bar', 2]
928
+ # a[6..50] = 'foo' # => "foo"
929
+ # a # => [:foo, "bar", 2, nil, nil, nil, "foo"]
930
+ #
931
+ # If `range.end` is zero, shifts elements at and following offset `start` and
932
+ # assigns `object` at offset `start`:
933
+ # a = [:foo, 'bar', 2]
934
+ # a[1..0] = 'foo' # => "foo"
935
+ # a # => [:foo, "foo", "bar", 2]
936
+ #
937
+ # If `range.end` is negative, assigns `object` at offset `start`, retains
938
+ # `range.end.abs -1` elements past that, and removes those beyond:
939
+ # a = [:foo, 'bar', 2]
940
+ # a[1..-1] = 'foo' # => "foo"
941
+ # a # => [:foo, "foo"]
942
+ # a = [:foo, 'bar', 2]
943
+ # a[1..-2] = 'foo' # => "foo"
944
+ # a # => [:foo, "foo", 2]
945
+ # a = [:foo, 'bar', 2]
946
+ # a[1..-3] = 'foo' # => "foo"
947
+ # a # => [:foo, "foo", "bar", 2]
948
+ # a = [:foo, 'bar', 2]
949
+ #
950
+ # If `range.end` is too large for the existing array, replaces array elements,
951
+ # but does not extend the array with `nil` values:
952
+ # a = [:foo, 'bar', 2]
953
+ # a[1..5] = 'foo' # => "foo"
954
+ # a # => [:foo, "foo"]
484
955
  #
485
956
  def []=: (int index, Elem obj) -> Elem
486
957
  | (int start, int length, Elem obj) -> Elem
@@ -490,425 +961,790 @@ class Array[unchecked out Elem] < Object
490
961
  | (::Range[::Integer?], ::Array[Elem]) -> ::Array[Elem]
491
962
  | (::Range[::Integer?], nil) -> nil
492
963
 
493
- # See also Enumerable#all?
964
+ # <!--
965
+ # rdoc-file=array.c
966
+ # - array.all? -> true or false
967
+ # - array.all? {|element| ... } -> true or false
968
+ # - array.all?(obj) -> true or false
969
+ # -->
970
+ # Returns `true` if all elements of `self` meet a given criterion.
971
+ #
972
+ # With no block given and no argument, returns `true` if `self` contains only
973
+ # truthy elements, `false` otherwise:
974
+ # [0, 1, :foo].all? # => true
975
+ # [0, nil, 2].all? # => false
976
+ # [].all? # => true
977
+ #
978
+ # With a block given and no argument, calls the block with each element in
979
+ # `self`; returns `true` if the block returns only truthy values, `false`
980
+ # otherwise:
981
+ # [0, 1, 2].all? { |element| element < 3 } # => true
982
+ # [0, 1, 2].all? { |element| element < 2 } # => false
983
+ #
984
+ # If argument `obj` is given, returns `true` if `obj.===` every element, `false`
985
+ # otherwise:
986
+ # ['food', 'fool', 'foot'].all?(/foo/) # => true
987
+ # ['food', 'drink'].all?(/bar/) # => false
988
+ # [].all?(/foo/) # => true
989
+ # [0, 0, 0].all?(0) # => true
990
+ # [0, 1, 2].all?(1) # => false
991
+ #
992
+ # Related: Enumerable#all?
494
993
  #
495
994
  def all?: () -> bool
496
995
  | (_Pattern[Elem] pattern) -> bool
497
996
  | () { (Elem obj) -> boolish } -> bool
498
997
 
499
- # See also Enumerable#any?
998
+ # <!--
999
+ # rdoc-file=array.c
1000
+ # - array.any? -> true or false
1001
+ # - array.any? {|element| ... } -> true or false
1002
+ # - array.any?(obj) -> true or false
1003
+ # -->
1004
+ # Returns `true` if any element of `self` meets a given criterion.
1005
+ #
1006
+ # With no block given and no argument, returns `true` if `self` has any truthy
1007
+ # element, `false` otherwise:
1008
+ # [nil, 0, false].any? # => true
1009
+ # [nil, false].any? # => false
1010
+ # [].any? # => false
1011
+ #
1012
+ # With a block given and no argument, calls the block with each element in
1013
+ # `self`; returns `true` if the block returns any truthy value, `false`
1014
+ # otherwise:
1015
+ # [0, 1, 2].any? {|element| element > 1 } # => true
1016
+ # [0, 1, 2].any? {|element| element > 2 } # => false
1017
+ #
1018
+ # If argument `obj` is given, returns `true` if `obj`.`===` any element, `false`
1019
+ # otherwise:
1020
+ # ['food', 'drink'].any?(/foo/) # => true
1021
+ # ['food', 'drink'].any?(/bar/) # => false
1022
+ # [].any?(/foo/) # => false
1023
+ # [0, 1, 2].any?(1) # => true
1024
+ # [0, 1, 2].any?(3) # => false
1025
+ #
1026
+ # Related: Enumerable#any?
500
1027
  #
501
1028
  alias any? all?
502
1029
 
503
- alias append push
504
-
505
- # Searches through an array whose elements are also arrays comparing `obj` with
506
- # the first element of each contained array using `obj.==`.
1030
+ # <!-- rdoc-file=array.c -->
1031
+ # Appends trailing elements.
507
1032
  #
508
- # Returns the first contained array that matches (that is, the first associated
509
- # array), or `nil` if no match is found.
1033
+ # Appends each argument in `objects` to `self`; returns `self`:
1034
+ # a = [:foo, 'bar', 2]
1035
+ # a.push(:baz, :bat) # => [:foo, "bar", 2, :baz, :bat]
510
1036
  #
511
- # See also Array#rassoc
1037
+ # Appends each argument as one element, even if it is another Array:
1038
+ # a = [:foo, 'bar', 2]
1039
+ # a1 = a.push([:baz, :bat], [:bam, :bad])
1040
+ # a1 # => [:foo, "bar", 2, [:baz, :bat], [:bam, :bad]]
512
1041
  #
513
- # s1 = [ "colors", "red", "blue", "green" ]
514
- # s2 = [ "letters", "a", "b", "c" ]
515
- # s3 = "foo"
516
- # a = [ s1, s2, s3 ]
517
- # a.assoc("letters") #=> [ "letters", "a", "b", "c" ]
518
- # a.assoc("foo") #=> nil
1042
+ # Array#append is an alias for Array#push.
519
1043
  #
520
- def assoc: (untyped) -> ::Array[untyped]?
521
-
522
- # Returns the element at `index`. A negative index counts from the end of
523
- # `self`. Returns `nil` if the index is out of range. See also Array#[].
1044
+ # Related: #pop, #shift, #unshift.
524
1045
  #
525
- # a = [ "a", "b", "c", "d", "e" ]
526
- # a.at(0) #=> "a"
527
- # a.at(-1) #=> "e"
528
- #
529
- def at: (int index) -> Elem?
1046
+ alias append push
530
1047
 
531
- # By using binary search, finds a value from this array which meets the given
532
- # condition in O(log n) where n is the size of the array.
533
- #
534
- # You can use this method in two modes: a find-minimum mode and a find-any mode.
535
- # In either case, the elements of the array must be monotone (or sorted) with
536
- # respect to the block.
1048
+ # <!--
1049
+ # rdoc-file=array.c
1050
+ # - array.assoc(obj) -> found_array or nil
1051
+ # -->
1052
+ # Returns the first element in `self` that is an Array whose first element `==`
1053
+ # `obj`:
1054
+ # a = [{foo: 0}, [2, 4], [4, 5, 6], [4, 5]]
1055
+ # a.assoc(4) # => [4, 5, 6]
537
1056
  #
538
- # In find-minimum mode (this is a good choice for typical use cases), the block
539
- # must always return true or false, and there must be an index i (0 <= i <=
540
- # ary.size) so that:
1057
+ # Returns `nil` if no such element is found.
541
1058
  #
542
- # * the block returns false for any element whose index is less than i, and
543
- # * the block returns true for any element whose index is greater than or
544
- # equal to i.
1059
+ # Related: #rassoc.
545
1060
  #
1061
+ def assoc: (untyped) -> ::Array[untyped]?
1062
+
1063
+ # <!--
1064
+ # rdoc-file=array.c
1065
+ # - array.at(index) -> object
1066
+ # -->
1067
+ # Returns the element at Integer offset `index`; does not modify `self`.
1068
+ # a = [:foo, 'bar', 2]
1069
+ # a.at(0) # => :foo
1070
+ # a.at(2) # => 2
546
1071
  #
547
- # This method returns the i-th element. If i is equal to ary.size, it returns
548
- # nil.
549
- #
550
- # ary = [0, 4, 7, 10, 12]
551
- # ary.bsearch {|x| x >= 4 } #=> 4
552
- # ary.bsearch {|x| x >= 6 } #=> 7
553
- # ary.bsearch {|x| x >= -1 } #=> 0
554
- # ary.bsearch {|x| x >= 100 } #=> nil
555
- #
556
- # In find-any mode (this behaves like libc's bsearch(3)), the block must always
557
- # return a number, and there must be two indices i and j (0 <= i <= j <=
558
- # ary.size) so that:
559
- #
560
- # * the block returns a positive number for [ary](k) if 0 <= k < i,
561
- # * the block returns zero for [ary](k) if i <= k < j, and
562
- # * the block returns a negative number for [ary](k) if j <= k < ary.size.
563
- #
564
- #
565
- # Under this condition, this method returns any element whose index is within
566
- # i...j. If i is equal to j (i.e., there is no element that satisfies the
567
- # block), this method returns nil.
568
- #
569
- # ary = [0, 4, 7, 10, 12]
570
- # # try to find v such that 4 <= v < 8
571
- # ary.bsearch {|x| 1 - x / 4 } #=> 4 or 7
572
- # # try to find v such that 8 <= v < 10
573
- # ary.bsearch {|x| 4 - x / 2 } #=> nil
1072
+ def at: (int index) -> Elem?
1073
+
1074
+ # <!--
1075
+ # rdoc-file=array.c
1076
+ # - array.bsearch {|element| ... } -> object
1077
+ # - array.bsearch -> new_enumerator
1078
+ # -->
1079
+ # Returns an element from `self` selected by a binary search.
574
1080
  #
575
- # You must not mix the two modes at a time; the block must always return either
576
- # true/false, or always return a number. It is undefined which value is
577
- # actually picked up at each iteration.
1081
+ # See [Binary Searching](rdoc-ref:bsearch.rdoc).
578
1082
  #
579
1083
  def bsearch: () -> ::Enumerator[Elem, Elem?]
580
1084
  | () { (Elem) -> (true | false) } -> Elem?
581
1085
  | () { (Elem) -> ::Integer } -> Elem?
582
1086
 
583
- # By using binary search, finds an index of a value from this array which meets
584
- # the given condition in O(log n) where n is the size of the array.
585
- #
586
- # It supports two modes, depending on the nature of the block. They are exactly
587
- # the same as in the case of the #bsearch method, with the only difference being
588
- # that this method returns the index of the element instead of the element
589
- # itself. For more details consult the documentation for #bsearch.
1087
+ # <!--
1088
+ # rdoc-file=array.c
1089
+ # - array.bsearch_index {|element| ... } -> integer or nil
1090
+ # - array.bsearch_index -> new_enumerator
1091
+ # -->
1092
+ # Searches `self` as described at method #bsearch, but returns the *index* of
1093
+ # the found element instead of the element itself.
590
1094
  #
591
1095
  def bsearch_index: () { (Elem) -> (true | false) } -> ::Integer?
592
1096
  | () { (Elem) -> ::Integer } -> ::Integer?
593
1097
 
594
- # Removes all elements from `self`.
595
- #
596
- # a = [ "a", "b", "c", "d", "e" ]
597
- # a.clear #=> [ ]
1098
+ # <!--
1099
+ # rdoc-file=array.c
1100
+ # - array.clear -> self
1101
+ # -->
1102
+ # Removes all elements from `self`:
1103
+ # a = [:foo, 'bar', 2]
1104
+ # a.clear # => []
598
1105
  #
599
1106
  def clear: () -> self
600
1107
 
601
- # Invokes the given block once for each element of `self`.
602
- #
603
- # Creates a new array containing the values returned by the block.
1108
+ # <!--
1109
+ # rdoc-file=array.c
1110
+ # - array.map {|element| ... } -> new_array
1111
+ # - array.map -> new_enumerator
1112
+ # -->
1113
+ # Calls the block, if given, with each element of `self`; returns a new Array
1114
+ # whose elements are the return values from the block:
1115
+ # a = [:foo, 'bar', 2]
1116
+ # a1 = a.map {|element| element.class }
1117
+ # a1 # => [Symbol, String, Integer]
604
1118
  #
605
- # See also Enumerable#collect.
1119
+ # Returns a new Enumerator if no block given:
1120
+ # a = [:foo, 'bar', 2]
1121
+ # a1 = a.map
1122
+ # a1 # => #<Enumerator: [:foo, "bar", 2]:map>
606
1123
  #
607
- # If no block is given, an Enumerator is returned instead.
608
- #
609
- # a = [ "a", "b", "c", "d" ]
610
- # a.collect {|x| x + "!"} #=> ["a!", "b!", "c!", "d!"]
611
- # a.map.with_index {|x, i| x * i} #=> ["", "b", "cc", "ddd"]
612
- # a #=> ["a", "b", "c", "d"]
1124
+ # Array#collect is an alias for Array#map.
613
1125
  #
614
1126
  def collect: [U] () { (Elem item) -> U } -> ::Array[U]
615
1127
  | () -> ::Enumerator[Elem, ::Array[untyped]]
616
1128
 
617
- # Invokes the given block once for each element of `self`, replacing the element
618
- # with the value returned by the block.
619
- #
620
- # See also Enumerable#collect.
1129
+ # <!--
1130
+ # rdoc-file=array.c
1131
+ # - array.map! {|element| ... } -> self
1132
+ # - array.map! -> new_enumerator
1133
+ # -->
1134
+ # Calls the block, if given, with each element; replaces the element with the
1135
+ # block's return value:
1136
+ # a = [:foo, 'bar', 2]
1137
+ # a.map! { |element| element.class } # => [Symbol, String, Integer]
621
1138
  #
622
- # If no block is given, an Enumerator is returned instead.
1139
+ # Returns a new Enumerator if no block given:
1140
+ # a = [:foo, 'bar', 2]
1141
+ # a1 = a.map!
1142
+ # a1 # => #<Enumerator: [:foo, "bar", 2]:map!>
623
1143
  #
624
- # a = [ "a", "b", "c", "d" ]
625
- # a.map! {|x| x + "!" }
626
- # a #=> [ "a!", "b!", "c!", "d!" ]
627
- # a.collect!.with_index {|x, i| x[0...i] }
628
- # a #=> ["", "b", "c!", "d!"]
1144
+ # Array#collect! is an alias for Array#map!.
629
1145
  #
630
- # collect! is monomorphic because of RBS limitation.
631
1146
  def collect!: () { (Elem item) -> Elem } -> self
632
1147
  | () -> ::Enumerator[Elem, self]
633
1148
 
634
- # When invoked with a block, yields all combinations of length `n` of elements
635
- # from the array and then returns the array itself.
1149
+ # <!--
1150
+ # rdoc-file=array.c
1151
+ # - array.combination(n) {|element| ... } -> self
1152
+ # - array.combination(n) -> new_enumerator
1153
+ # -->
1154
+ # Calls the block, if given, with combinations of elements of `self`; returns
1155
+ # `self`. The order of combinations is indeterminate.
636
1156
  #
637
- # The implementation makes no guarantees about the order in which the
638
- # combinations are yielded.
1157
+ # When a block and an in-range positive Integer argument `n` (`0 < n <=
1158
+ # self.size`) are given, calls the block with all `n`-tuple combinations of
1159
+ # `self`.
639
1160
  #
640
- # If no block is given, an Enumerator is returned instead.
1161
+ # Example:
1162
+ # a = [0, 1, 2]
1163
+ # a.combination(2) {|combination| p combination }
641
1164
  #
642
- # Examples:
1165
+ # Output:
1166
+ # [0, 1]
1167
+ # [0, 2]
1168
+ # [1, 2]
1169
+ #
1170
+ # Another example:
1171
+ # a = [0, 1, 2]
1172
+ # a.combination(3) {|combination| p combination }
1173
+ #
1174
+ # Output:
1175
+ # [0, 1, 2]
643
1176
  #
644
- # a = [1, 2, 3, 4]
645
- # a.combination(1).to_a #=> [[1],[2],[3],[4]]
646
- # a.combination(2).to_a #=> [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]
647
- # a.combination(3).to_a #=> [[1,2,3],[1,2,4],[1,3,4],[2,3,4]]
648
- # a.combination(4).to_a #=> [[1,2,3,4]]
649
- # a.combination(0).to_a #=> [[]] # one combination of length 0
650
- # a.combination(5).to_a #=> [] # no combinations of length 5
1177
+ # When `n` is zero, calls the block once with a new empty Array:
1178
+ # a = [0, 1, 2]
1179
+ # a1 = a.combination(0) {|combination| p combination }
1180
+ #
1181
+ # Output:
1182
+ # []
1183
+ #
1184
+ # When `n` is out of range (negative or larger than `self.size`), does not call
1185
+ # the block:
1186
+ # a = [0, 1, 2]
1187
+ # a.combination(-1) {|combination| fail 'Cannot happen' }
1188
+ # a.combination(4) {|combination| fail 'Cannot happen' }
1189
+ #
1190
+ # Returns a new Enumerator if no block given:
1191
+ # a = [0, 1, 2]
1192
+ # a.combination(2) # => #<Enumerator: [0, 1, 2]:combination(2)>
651
1193
  #
652
1194
  def combination: (int n) { (::Array[Elem]) -> void } -> self
653
1195
  | (int n) -> ::Enumerator[::Array[Elem], self]
654
1196
 
655
- # Returns a copy of `self` with all `nil` elements removed.
656
- #
657
- # [ "a", nil, "b", nil, "c", nil ].compact
658
- # #=> [ "a", "b", "c" ]
1197
+ # <!--
1198
+ # rdoc-file=array.c
1199
+ # - array.compact -> new_array
1200
+ # -->
1201
+ # Returns a new Array containing all non-`nil` elements from `self`:
1202
+ # a = [nil, 0, nil, 1, nil, 2, nil]
1203
+ # a.compact # => [0, 1, 2]
659
1204
  #
660
1205
  def compact: () -> ::Array[Elem]
661
1206
 
662
- # Removes `nil` elements from the array.
663
- #
664
- # Returns `nil` if no changes were made, otherwise returns the array.
1207
+ # <!--
1208
+ # rdoc-file=array.c
1209
+ # - array.compact! -> self or nil
1210
+ # -->
1211
+ # Removes all `nil` elements from `self`.
665
1212
  #
666
- # [ "a", nil, "b", nil, "c" ].compact! #=> [ "a", "b", "c" ]
667
- # [ "a", "b", "c" ].compact! #=> nil
1213
+ # Returns `self` if any elements removed, otherwise `nil`.
668
1214
  #
669
1215
  def compact!: () -> self?
670
1216
 
671
- # Appends the elements of `other_ary`s to `self`.
672
- #
673
- # [ "a", "b" ].concat( ["c", "d"]) #=> [ "a", "b", "c", "d" ]
674
- # [ "a" ].concat( ["b"], ["c", "d"]) #=> [ "a", "b", "c", "d" ]
675
- # [ "a" ].concat #=> [ "a" ]
676
- #
677
- # a = [ 1, 2, 3 ]
678
- # a.concat( [ 4, 5 ])
679
- # a #=> [ 1, 2, 3, 4, 5 ]
680
- #
681
- # a = [ 1, 2 ]
682
- # a.concat(a, a) #=> [1, 2, 1, 2, 1, 2]
683
- #
684
- # See also Array#+.
1217
+ # <!--
1218
+ # rdoc-file=array.c
1219
+ # - array.concat(*other_arrays) -> self
1220
+ # -->
1221
+ # Adds to `array` all elements from each Array in `other_arrays`; returns
1222
+ # `self`:
1223
+ # a = [0, 1]
1224
+ # a.concat([2, 3], [4, 5]) # => [0, 1, 2, 3, 4, 5]
685
1225
  #
686
1226
  def concat: (*::Array[Elem] arrays) -> self
687
1227
 
688
- # Returns the number of elements.
1228
+ # <!--
1229
+ # rdoc-file=array.c
1230
+ # - array.count -> an_integer
1231
+ # - array.count(obj) -> an_integer
1232
+ # - array.count {|element| ... } -> an_integer
1233
+ # -->
1234
+ # Returns a count of specified elements.
689
1235
  #
690
- # If an argument is given, counts the number of elements which equal `obj` using
691
- # `==`.
1236
+ # With no argument and no block, returns the count of all elements:
1237
+ # [0, 1, 2].count # => 3
1238
+ # [].count # => 0
692
1239
  #
693
- # If a block is given, counts the number of elements for which the block returns
694
- # a true value.
1240
+ # With argument `obj`, returns the count of elements `==` to `obj`:
1241
+ # [0, 1, 2, 0.0].count(0) # => 2
1242
+ # [0, 1, 2].count(3) # => 0
695
1243
  #
696
- # ary = [1, 2, 4, 2]
697
- # ary.count #=> 4
698
- # ary.count(2) #=> 2
699
- # ary.count {|x| x%2 == 0} #=> 3
1244
+ # With no argument and a block given, calls the block with each element; returns
1245
+ # the count of elements for which the block returns a truthy value:
1246
+ # [0, 1, 2, 3].count {|element| element > 1} # => 2
1247
+ #
1248
+ # With argument `obj` and a block given, issues a warning, ignores the block,
1249
+ # and returns the count of elements `==` to `obj`:
700
1250
  #
701
1251
  def count: () -> ::Integer
702
1252
  | (Elem obj) -> ::Integer
703
1253
  | () { (Elem) -> boolish } -> ::Integer
704
1254
 
705
- # Calls the given block for each element `n` times or forever if `nil` is given.
706
- #
707
- # Does nothing if a non-positive number is given or the array is empty.
708
- #
709
- # Returns `nil` if the loop has finished without getting interrupted.
710
- #
711
- # If no block is given, an Enumerator is returned instead.
712
- #
713
- # a = ["a", "b", "c"]
714
- # a.cycle {|x| puts x} # print, a, b, c, a, b, c,.. forever.
715
- # a.cycle(2) {|x| puts x} # print, a, b, c, a, b, c.
1255
+ # <!--
1256
+ # rdoc-file=array.c
1257
+ # - array.cycle {|element| ... } -> nil
1258
+ # - array.cycle(count) {|element| ... } -> nil
1259
+ # - array.cycle -> new_enumerator
1260
+ # - array.cycle(count) -> new_enumerator
1261
+ # -->
1262
+ # When called with positive Integer argument `count` and a block, calls the
1263
+ # block with each element, then does so again, until it has done so `count`
1264
+ # times; returns `nil`:
1265
+ # output = []
1266
+ # [0, 1].cycle(2) {|element| output.push(element) } # => nil
1267
+ # output # => [0, 1, 0, 1]
1268
+ #
1269
+ # If `count` is zero or negative, does not call the block:
1270
+ # [0, 1].cycle(0) {|element| fail 'Cannot happen' } # => nil
1271
+ # [0, 1].cycle(-1) {|element| fail 'Cannot happen' } # => nil
1272
+ #
1273
+ # When a block is given, and argument is omitted or `nil`, cycles forever:
1274
+ # # Prints 0 and 1 forever.
1275
+ # [0, 1].cycle {|element| puts element }
1276
+ # [0, 1].cycle(nil) {|element| puts element }
1277
+ #
1278
+ # When no block is given, returns a new Enumerator:
1279
+ #
1280
+ # [0, 1].cycle(2) # => #<Enumerator: [0, 1]:cycle(2)>
1281
+ # [0, 1].cycle # => # => #<Enumerator: [0, 1]:cycle>
1282
+ # [0, 1].cycle.first(5) # => [0, 1, 0, 1, 0]
716
1283
  #
717
1284
  def cycle: (?int? n) { (Elem) -> void } -> nil
718
1285
  | (?int? n) -> ::Enumerator[Elem, nil]
719
1286
 
1287
+ # <!--
1288
+ # rdoc-file=array.c
1289
+ # - deconstruct()
1290
+ # -->
1291
+ #
720
1292
  def deconstruct: () -> self
721
1293
 
722
- # Deletes all items from `self` that are equal to `obj`.
1294
+ # <!--
1295
+ # rdoc-file=array.c
1296
+ # - array.delete(obj) -> deleted_object
1297
+ # - array.delete(obj) {|nosuch| ... } -> deleted_object or block_return
1298
+ # -->
1299
+ # Removes zero or more elements from `self`; returns `self`.
1300
+ #
1301
+ # When no block is given, removes from `self` each element `ele` such that `ele
1302
+ # == obj`; returns the last deleted element:
1303
+ # s1 = 'bar'; s2 = 'bar'
1304
+ # a = [:foo, s1, 2, s2]
1305
+ # a.delete('bar') # => "bar"
1306
+ # a # => [:foo, 2]
723
1307
  #
724
- # Returns the last deleted item, or `nil` if no matching item is found.
1308
+ # Returns `nil` if no elements removed.
725
1309
  #
726
- # If the optional code block is given, the result of the block is returned if
727
- # the item is not found. (To remove `nil` elements and get an informative
728
- # return value, use Array#compact!)
1310
+ # When a block is given, removes from `self` each element `ele` such that `ele
1311
+ # == obj`.
729
1312
  #
730
- # a = [ "a", "b", "b", "b", "c" ]
731
- # a.delete("b") #=> "b"
732
- # a #=> ["a", "c"]
733
- # a.delete("z") #=> nil
734
- # a.delete("z") {"not found"} #=> "not found"
1313
+ # If any such elements are found, ignores the block and returns the last deleted
1314
+ # element:
1315
+ # s1 = 'bar'; s2 = 'bar'
1316
+ # a = [:foo, s1, 2, s2]
1317
+ # deleted_obj = a.delete('bar') {|obj| fail 'Cannot happen' }
1318
+ # a # => [:foo, 2]
1319
+ #
1320
+ # If no such elements are found, returns the block's return value:
1321
+ # a = [:foo, 'bar', 2]
1322
+ # a.delete(:nosuch) {|obj| "#{obj} not found" } # => "nosuch not found"
735
1323
  #
736
1324
  def delete: (Elem obj) -> Elem?
737
1325
  | [S, T] (S obj) { (S) -> T } -> (Elem | T)
738
1326
 
739
- # Deletes the element at the specified `index`, returning that element, or `nil`
740
- # if the `index` is out of range.
741
- #
742
- # See also Array#slice!
1327
+ # <!--
1328
+ # rdoc-file=array.c
1329
+ # - array.delete_at(index) -> deleted_object or nil
1330
+ # -->
1331
+ # Deletes an element from `self`, per the given Integer `index`.
743
1332
  #
744
- # a = ["ant", "bat", "cat", "dog"]
745
- # a.delete_at(2) #=> "cat"
746
- # a #=> ["ant", "bat", "dog"]
747
- # a.delete_at(99) #=> nil
1333
+ # When `index` is non-negative, deletes the element at offset `index`:
1334
+ # a = [:foo, 'bar', 2]
1335
+ # a.delete_at(1) # => "bar"
1336
+ # a # => [:foo, 2]
748
1337
  #
749
- def delete_at: (int index) -> Elem?
750
-
751
- # Deletes every element of `self` for which block evaluates to `true`.
1338
+ # If index is too large, returns `nil`.
752
1339
  #
753
- # The array is changed instantly every time the block is called, not after the
754
- # iteration is over.
1340
+ # When `index` is negative, counts backward from the end of the array:
1341
+ # a = [:foo, 'bar', 2]
1342
+ # a.delete_at(-2) # => "bar"
1343
+ # a # => [:foo, 2]
755
1344
  #
756
- # See also Array#reject!
1345
+ # If `index` is too small (far from zero), returns nil.
757
1346
  #
758
- # If no block is given, an Enumerator is returned instead.
1347
+ def delete_at: (int index) -> Elem?
1348
+
1349
+ # <!--
1350
+ # rdoc-file=array.c
1351
+ # - array.delete_if {|element| ... } -> self
1352
+ # - array.delete_if -> Enumerator
1353
+ # -->
1354
+ # Removes each element in `self` for which the block returns a truthy value;
1355
+ # returns `self`:
1356
+ # a = [:foo, 'bar', 2, 'bat']
1357
+ # a.delete_if {|element| element.to_s.start_with?('b') } # => [:foo, 2]
759
1358
  #
760
- # scores = [ 97, 42, 75 ]
761
- # scores.delete_if {|score| score < 80 } #=> [97]
1359
+ # Returns a new Enumerator if no block given:
1360
+ # a = [:foo, 'bar', 2]
1361
+ # a.delete_if # => #<Enumerator: [:foo, "bar", 2]:delete_if>
762
1362
  #
763
1363
  def delete_if: () { (Elem item) -> boolish } -> self
764
1364
  | () -> ::Enumerator[Elem, self]
765
1365
 
766
- # Array Difference
767
- #
768
- # Returns a new array that is a copy of the original array, removing all
769
- # occurrences of any item that also appear in `other_ary`. The order is
770
- # preserved from the original array.
771
- #
772
- # It compares elements using their #hash and #eql? methods for efficiency.
773
- #
774
- # [ 1, 1, 2, 2, 3, 3, 4, 5 ].difference([ 1, 2, 4 ]) #=> [ 3, 3, 5 ]
775
- #
776
- # Note that while 1 and 2 were only present once in the array argument, and were
777
- # present twice in the receiver array, all occurrences of each Integer are
778
- # removed in the returned array.
779
- #
780
- # Multiple array arguments can be supplied and all occurrences of any element in
781
- # those supplied arrays that match the receiver will be removed from the
782
- # returned array.
783
- #
784
- # [ 1, 'c', :s, 'yep' ].difference([ 1 ], [ 'a', 'c' ]) #=> [ :s, "yep" ]
1366
+ # <!--
1367
+ # rdoc-file=array.c
1368
+ # - array.difference(*other_arrays) -> new_array
1369
+ # -->
1370
+ # Returns a new Array containing only those elements from `self` that are not
1371
+ # found in any of the Arrays `other_arrays`; items are compared using `eql?`;
1372
+ # order from `self` is preserved:
1373
+ # [0, 1, 1, 2, 1, 1, 3, 1, 1].difference([1]) # => [0, 2, 3]
1374
+ # [0, 1, 2, 3].difference([3, 0], [1, 3]) # => [2]
1375
+ # [0, 1, 2].difference([4]) # => [0, 1, 2]
785
1376
  #
786
- # If you need set-like behavior, see the library class Set.
1377
+ # Returns a copy of `self` if no arguments given.
787
1378
  #
788
- # See also Array#-.
1379
+ # Related: Array#-.
789
1380
  #
790
1381
  def difference: (*::Array[untyped] arrays) -> ::Array[Elem]
791
1382
 
792
- # Extracts the nested value specified by the sequence of *idx* objects by
793
- # calling `dig` at each step, returning `nil` if any intermediate step is `nil`.
1383
+ # <!--
1384
+ # rdoc-file=array.c
1385
+ # - array.dig(index, *identifiers) -> object
1386
+ # -->
1387
+ # Finds and returns the object in nested objects that is specified by `index`
1388
+ # and `identifiers`. The nested objects may be instances of various classes. See
1389
+ # [Dig Methods](rdoc-ref:dig_methods.rdoc).
794
1390
  #
795
- # a = [[1, [2, 3]]]
796
- #
797
- # a.dig(0, 1, 1) #=> 3
798
- # a.dig(1, 2, 3) #=> nil
799
- # a.dig(0, 0, 0) #=> TypeError: Integer does not have #dig method
800
- # [42, {foo: :bar}].dig(1, :foo) #=> :bar
1391
+ # Examples:
1392
+ # a = [:foo, [:bar, :baz, [:bat, :bam]]]
1393
+ # a.dig(1) # => [:bar, :baz, [:bat, :bam]]
1394
+ # a.dig(1, 2) # => [:bat, :bam]
1395
+ # a.dig(1, 2, 0) # => :bat
1396
+ # a.dig(1, 2, 3) # => nil
801
1397
  #
802
1398
  def dig: (int idx) -> Elem?
803
1399
  | (int idx, untyped, *untyped) -> untyped
804
1400
 
805
- # Drops first `n` elements from `ary` and returns the rest of the elements in an
806
- # array.
807
- #
808
- # If a negative number is given, raises an ArgumentError.
1401
+ # <!--
1402
+ # rdoc-file=array.c
1403
+ # - array.drop(n) -> new_array
1404
+ # -->
1405
+ # Returns a new Array containing all but the first `n` element of `self`, where
1406
+ # `n` is a non-negative Integer; does not modify `self`.
809
1407
  #
810
- # See also Array#take
811
- #
812
- # a = [1, 2, 3, 4, 5, 0]
813
- # a.drop(3) #=> [4, 5, 0]
1408
+ # Examples:
1409
+ # a = [0, 1, 2, 3, 4, 5]
1410
+ # a.drop(0) # => [0, 1, 2, 3, 4, 5]
1411
+ # a.drop(1) # => [1, 2, 3, 4, 5]
1412
+ # a.drop(2) # => [2, 3, 4, 5]
814
1413
  #
815
1414
  def drop: (int n) -> ::Array[Elem]
816
1415
 
817
- # Drops elements up to, but not including, the first element for which the block
818
- # returns `nil` or `false` and returns an array containing the remaining
819
- # elements.
1416
+ # <!--
1417
+ # rdoc-file=array.c
1418
+ # - array.drop_while {|element| ... } -> new_array
1419
+ # - array.drop_while -> new_enumerator
1420
+ # -->
1421
+ # Returns a new Array containing zero or more trailing elements of `self`; does
1422
+ # not modify `self`.
820
1423
  #
821
- # If no block is given, an Enumerator is returned instead.
1424
+ # With a block given, calls the block with each successive element of `self`;
1425
+ # stops if the block returns `false` or `nil`; returns a new Array *omitting*
1426
+ # those elements for which the block returned a truthy value:
1427
+ # a = [0, 1, 2, 3, 4, 5]
1428
+ # a.drop_while {|element| element < 3 } # => [3, 4, 5]
822
1429
  #
823
- # See also Array#take_while
824
- #
825
- # a = [1, 2, 3, 4, 5, 0]
826
- # a.drop_while {|i| i < 3 } #=> [3, 4, 5, 0]
1430
+ # With no block given, returns a new Enumerator:
1431
+ # [0, 1].drop_while # => # => #<Enumerator: [0, 1]:drop_while>
827
1432
  #
828
1433
  def drop_while: () { (Elem obj) -> boolish } -> ::Array[Elem]
829
1434
  | () -> ::Enumerator[Elem, ::Array[Elem]]
830
1435
 
831
- # Calls the given block once for each element in `self`, passing that element as
832
- # a parameter. Returns the array itself.
1436
+ # <!--
1437
+ # rdoc-file=array.c
1438
+ # - array.each {|element| ... } -> self
1439
+ # - array.each -> Enumerator
1440
+ # -->
1441
+ # Iterates over array elements.
833
1442
  #
834
- # If no block is given, an Enumerator is returned.
1443
+ # When a block given, passes each successive array element to the block; returns
1444
+ # `self`:
1445
+ # a = [:foo, 'bar', 2]
1446
+ # a.each {|element| puts "#{element.class} #{element}" }
835
1447
  #
836
- # a = [ "a", "b", "c" ]
837
- # a.each {|x| print x, " -- " }
1448
+ # Output:
1449
+ # Symbol foo
1450
+ # String bar
1451
+ # Integer 2
1452
+ #
1453
+ # Allows the array to be modified during iteration:
1454
+ # a = [:foo, 'bar', 2]
1455
+ # a.each {|element| puts element; a.clear if element.to_s.start_with?('b') }
1456
+ #
1457
+ # Output:
1458
+ # foo
1459
+ # bar
838
1460
  #
839
- # produces:
1461
+ # When no block given, returns a new Enumerator:
1462
+ # a = [:foo, 'bar', 2]
1463
+ # e = a.each
1464
+ # e # => #<Enumerator: [:foo, "bar", 2]:each>
1465
+ # a1 = e.each {|element| puts "#{element.class} #{element}" }
840
1466
  #
841
- # a -- b -- c --
1467
+ # Output:
1468
+ # Symbol foo
1469
+ # String bar
1470
+ # Integer 2
1471
+ #
1472
+ # Related: #each_index, #reverse_each.
842
1473
  #
843
1474
  def each: () -> ::Enumerator[Elem, self]
844
1475
  | () { (Elem item) -> void } -> self
845
1476
 
846
- # Same as Array#each, but passes the `index` of the element instead of the
847
- # element itself.
1477
+ # <!--
1478
+ # rdoc-file=array.c
1479
+ # - array.each_index {|index| ... } -> self
1480
+ # - array.each_index -> Enumerator
1481
+ # -->
1482
+ # Iterates over array indexes.
848
1483
  #
849
- # An Enumerator is returned if no block is given.
1484
+ # When a block given, passes each successive array index to the block; returns
1485
+ # `self`:
1486
+ # a = [:foo, 'bar', 2]
1487
+ # a.each_index {|index| puts "#{index} #{a[index]}" }
850
1488
  #
851
- # a = [ "a", "b", "c" ]
852
- # a.each_index {|x| print x, " -- " }
1489
+ # Output:
1490
+ # 0 foo
1491
+ # 1 bar
1492
+ # 2 2
1493
+ #
1494
+ # Allows the array to be modified during iteration:
1495
+ # a = [:foo, 'bar', 2]
1496
+ # a.each_index {|index| puts index; a.clear if index > 0 }
1497
+ #
1498
+ # Output:
1499
+ # 0
1500
+ # 1
1501
+ #
1502
+ # When no block given, returns a new Enumerator:
1503
+ # a = [:foo, 'bar', 2]
1504
+ # e = a.each_index
1505
+ # e # => #<Enumerator: [:foo, "bar", 2]:each_index>
1506
+ # a1 = e.each {|index| puts "#{index} #{a[index]}"}
853
1507
  #
854
- # produces:
1508
+ # Output:
1509
+ # 0 foo
1510
+ # 1 bar
1511
+ # 2 2
855
1512
  #
856
- # 0 -- 1 -- 2 --
1513
+ # Related: #each, #reverse_each.
857
1514
  #
858
1515
  def each_index: () { (::Integer index) -> void } -> self
859
1516
  | () -> ::Enumerator[::Integer, self]
860
1517
 
861
- # Returns `true` if `self` contains no elements.
862
- #
863
- # [].empty? #=> true
1518
+ # <!--
1519
+ # rdoc-file=array.c
1520
+ # - array.empty? -> true or false
1521
+ # -->
1522
+ # Returns `true` if the count of elements in `self` is zero, `false` otherwise.
864
1523
  #
865
1524
  def empty?: () -> bool
866
1525
 
867
- # Returns `true` if `self` and `other` are the same object, or are both arrays
868
- # with the same content (according to Object#eql?).
1526
+ # <!--
1527
+ # rdoc-file=array.c
1528
+ # - array.eql? other_array -> true or false
1529
+ # -->
1530
+ # Returns `true` if `self` and `other_array` are the same size, and if, for each
1531
+ # index `i` in `self`, `self[i].eql? other_array[i]`:
1532
+ # a0 = [:foo, 'bar', 2]
1533
+ # a1 = [:foo, 'bar', 2]
1534
+ # a1.eql?(a0) # => true
869
1535
  #
870
- def eql?: (untyped other) -> bool
871
-
872
- # Tries to return the element at position `index`, but throws an IndexError
873
- # exception if the referenced `index` lies outside of the array bounds. This
874
- # error can be prevented by supplying a second argument, which will act as a
875
- # `default` value.
1536
+ # Otherwise, returns `false`.
876
1537
  #
877
- # Alternatively, if a block is given it will only be executed when an invalid
878
- # `index` is referenced.
1538
+ # This method is different from method [Array#==](#method-i-3D-3D), which
1539
+ # compares using method `Object#==`.
879
1540
  #
880
- # Negative values of `index` count from the end of the array.
881
- #
882
- # a = [ 11, 22, 33, 44 ]
883
- # a.fetch(1) #=> 22
884
- # a.fetch(-1) #=> 44
885
- # a.fetch(4, 'cat') #=> "cat"
886
- # a.fetch(100) {|i| puts "#{i} is out of bounds"}
887
- # #=> "100 is out of bounds"
1541
+ def eql?: (untyped other) -> bool
1542
+
1543
+ # <!--
1544
+ # rdoc-file=array.c
1545
+ # - array.fetch(index) -> element
1546
+ # - array.fetch(index, default_value) -> element
1547
+ # - array.fetch(index) {|index| ... } -> element
1548
+ # -->
1549
+ # Returns the element at offset `index`.
1550
+ #
1551
+ # With the single Integer argument `index`, returns the element at offset
1552
+ # `index`:
1553
+ # a = [:foo, 'bar', 2]
1554
+ # a.fetch(1) # => "bar"
1555
+ #
1556
+ # If `index` is negative, counts from the end of the array:
1557
+ # a = [:foo, 'bar', 2]
1558
+ # a.fetch(-1) # => 2
1559
+ # a.fetch(-2) # => "bar"
1560
+ #
1561
+ # With arguments `index` and `default_value`, returns the element at offset
1562
+ # `index` if index is in range, otherwise returns `default_value`:
1563
+ # a = [:foo, 'bar', 2]
1564
+ # a.fetch(1, nil) # => "bar"
1565
+ #
1566
+ # With argument `index` and a block, returns the element at offset `index` if
1567
+ # index is in range (and the block is not called); otherwise calls the block
1568
+ # with index and returns its return value:
1569
+ #
1570
+ # a = [:foo, 'bar', 2]
1571
+ # a.fetch(1) {|index| raise 'Cannot happen' } # => "bar"
1572
+ # a.fetch(50) {|index| "Value for #{index}" } # => "Value for 50"
888
1573
  #
889
1574
  def fetch: (int index) -> Elem
890
1575
  | [T] (int index, T default) -> (Elem | T)
891
1576
  | [T] (int index) { (int index) -> T } -> (Elem | T)
892
1577
 
893
- # The first three forms set the selected elements of `self` (which may be the
894
- # entire array) to `obj`.
895
- #
896
- # A `start` of `nil` is equivalent to zero.
897
- #
898
- # A `length` of `nil` is equivalent to the length of the array.
899
- #
900
- # The last three forms fill the array with the value of the given block, which
901
- # is passed the absolute index of each element to be filled.
902
- #
903
- # Negative values of `start` count from the end of the array, where `-1` is the
904
- # last element.
905
- #
906
- # a = [ "a", "b", "c", "d" ]
907
- # a.fill("x") #=> ["x", "x", "x", "x"]
908
- # a.fill("z", 2, 2) #=> ["x", "x", "z", "z"]
909
- # a.fill("y", 0..1) #=> ["y", "y", "z", "z"]
910
- # a.fill {|i| i*i} #=> [0, 1, 4, 9]
911
- # a.fill(-2) {|i| i*i*i} #=> [0, 1, 8, 27]
1578
+ # <!--
1579
+ # rdoc-file=array.c
1580
+ # - array.fill(obj) -> self
1581
+ # - array.fill(obj, start) -> self
1582
+ # - array.fill(obj, start, length) -> self
1583
+ # - array.fill(obj, range) -> self
1584
+ # - array.fill {|index| ... } -> self
1585
+ # - array.fill(start) {|index| ... } -> self
1586
+ # - array.fill(start, length) {|index| ... } -> self
1587
+ # - array.fill(range) {|index| ... } -> self
1588
+ # -->
1589
+ # Replaces specified elements in `self` with specified objects; returns `self`.
1590
+ #
1591
+ # With argument `obj` and no block given, replaces all elements with that one
1592
+ # object:
1593
+ # a = ['a', 'b', 'c', 'd']
1594
+ # a # => ["a", "b", "c", "d"]
1595
+ # a.fill(:X) # => [:X, :X, :X, :X]
1596
+ #
1597
+ # With arguments `obj` and Integer `start`, and no block given, replaces
1598
+ # elements based on the given start.
1599
+ #
1600
+ # If `start` is in range (`0 <= start < array.size`), replaces all elements from
1601
+ # offset `start` through the end:
1602
+ # a = ['a', 'b', 'c', 'd']
1603
+ # a.fill(:X, 2) # => ["a", "b", :X, :X]
1604
+ #
1605
+ # If `start` is too large (`start >= array.size`), does nothing:
1606
+ # a = ['a', 'b', 'c', 'd']
1607
+ # a.fill(:X, 4) # => ["a", "b", "c", "d"]
1608
+ # a = ['a', 'b', 'c', 'd']
1609
+ # a.fill(:X, 5) # => ["a", "b", "c", "d"]
1610
+ #
1611
+ # If `start` is negative, counts from the end (starting index is `start +
1612
+ # array.size`):
1613
+ # a = ['a', 'b', 'c', 'd']
1614
+ # a.fill(:X, -2) # => ["a", "b", :X, :X]
1615
+ #
1616
+ # If `start` is too small (less than and far from zero), replaces all elements:
1617
+ # a = ['a', 'b', 'c', 'd']
1618
+ # a.fill(:X, -6) # => [:X, :X, :X, :X]
1619
+ # a = ['a', 'b', 'c', 'd']
1620
+ # a.fill(:X, -50) # => [:X, :X, :X, :X]
1621
+ #
1622
+ # With arguments `obj`, Integer `start`, and Integer `length`, and no block
1623
+ # given, replaces elements based on the given `start` and `length`.
1624
+ #
1625
+ # If `start` is in range, replaces `length` elements beginning at offset
1626
+ # `start`:
1627
+ # a = ['a', 'b', 'c', 'd']
1628
+ # a.fill(:X, 1, 1) # => ["a", :X, "c", "d"]
1629
+ #
1630
+ # If `start` is negative, counts from the end:
1631
+ # a = ['a', 'b', 'c', 'd']
1632
+ # a.fill(:X, -2, 1) # => ["a", "b", :X, "d"]
1633
+ #
1634
+ # If `start` is large (`start >= array.size`), extends `self` with `nil`:
1635
+ # a = ['a', 'b', 'c', 'd']
1636
+ # a.fill(:X, 5, 0) # => ["a", "b", "c", "d", nil]
1637
+ # a = ['a', 'b', 'c', 'd']
1638
+ # a.fill(:X, 5, 2) # => ["a", "b", "c", "d", nil, :X, :X]
1639
+ #
1640
+ # If `length` is zero or negative, replaces no elements:
1641
+ # a = ['a', 'b', 'c', 'd']
1642
+ # a.fill(:X, 1, 0) # => ["a", "b", "c", "d"]
1643
+ # a.fill(:X, 1, -1) # => ["a", "b", "c", "d"]
1644
+ #
1645
+ # With arguments `obj` and Range `range`, and no block given, replaces elements
1646
+ # based on the given range.
1647
+ #
1648
+ # If the range is positive and ascending (`0 < range.begin <= range.end`),
1649
+ # replaces elements from `range.begin` to `range.end`:
1650
+ # a = ['a', 'b', 'c', 'd']
1651
+ # a.fill(:X, (1..1)) # => ["a", :X, "c", "d"]
1652
+ #
1653
+ # If `range.first` is negative, replaces no elements:
1654
+ # a = ['a', 'b', 'c', 'd']
1655
+ # a.fill(:X, (-1..1)) # => ["a", "b", "c", "d"]
1656
+ #
1657
+ # If `range.last` is negative, counts from the end:
1658
+ # a = ['a', 'b', 'c', 'd']
1659
+ # a.fill(:X, (0..-2)) # => [:X, :X, :X, "d"]
1660
+ # a = ['a', 'b', 'c', 'd']
1661
+ # a.fill(:X, (1..-2)) # => ["a", :X, :X, "d"]
1662
+ #
1663
+ # If `range.last` and `range.last` are both negative, both count from the end of
1664
+ # the array:
1665
+ # a = ['a', 'b', 'c', 'd']
1666
+ # a.fill(:X, (-1..-1)) # => ["a", "b", "c", :X]
1667
+ # a = ['a', 'b', 'c', 'd']
1668
+ # a.fill(:X, (-2..-2)) # => ["a", "b", :X, "d"]
1669
+ #
1670
+ # With no arguments and a block given, calls the block with each index; replaces
1671
+ # the corresponding element with the block's return value:
1672
+ # a = ['a', 'b', 'c', 'd']
1673
+ # a.fill { |index| "new_#{index}" } # => ["new_0", "new_1", "new_2", "new_3"]
1674
+ #
1675
+ # With argument `start` and a block given, calls the block with each index from
1676
+ # offset `start` to the end; replaces the corresponding element with the block's
1677
+ # return value:
1678
+ #
1679
+ # If start is in range (`0 <= start < array.size`), replaces from offset `start`
1680
+ # to the end:
1681
+ # a = ['a', 'b', 'c', 'd']
1682
+ # a.fill(1) { |index| "new_#{index}" } # => ["a", "new_1", "new_2", "new_3"]
1683
+ #
1684
+ # If `start` is too large(`start >= array.size`), does nothing:
1685
+ # a = ['a', 'b', 'c', 'd']
1686
+ # a.fill(4) { |index| fail 'Cannot happen' } # => ["a", "b", "c", "d"]
1687
+ # a = ['a', 'b', 'c', 'd']
1688
+ # a.fill(4) { |index| fail 'Cannot happen' } # => ["a", "b", "c", "d"]
1689
+ #
1690
+ # If `start` is negative, counts from the end:
1691
+ # a = ['a', 'b', 'c', 'd']
1692
+ # a.fill(-2) { |index| "new_#{index}" } # => ["a", "b", "new_2", "new_3"]
1693
+ #
1694
+ # If start is too small (`start <= -array.size`, replaces all elements:
1695
+ # a = ['a', 'b', 'c', 'd']
1696
+ # a.fill(-6) { |index| "new_#{index}" } # => ["new_0", "new_1", "new_2", "new_3"]
1697
+ # a = ['a', 'b', 'c', 'd']
1698
+ # a.fill(-50) { |index| "new_#{index}" } # => ["new_0", "new_1", "new_2", "new_3"]
1699
+ #
1700
+ # With arguments `start` and `length`, and a block given, calls the block for
1701
+ # each index specified by start length; replaces the corresponding element with
1702
+ # the block's return value.
1703
+ #
1704
+ # If `start` is in range, replaces `length` elements beginning at offset
1705
+ # `start`:
1706
+ # a = ['a', 'b', 'c', 'd']
1707
+ # a.fill(1, 1) { |index| "new_#{index}" } # => ["a", "new_1", "c", "d"]
1708
+ #
1709
+ # If start is negative, counts from the end:
1710
+ # a = ['a', 'b', 'c', 'd']
1711
+ # a.fill(-2, 1) { |index| "new_#{index}" } # => ["a", "b", "new_2", "d"]
1712
+ #
1713
+ # If `start` is large (`start >= array.size`), extends `self` with `nil`:
1714
+ # a = ['a', 'b', 'c', 'd']
1715
+ # a.fill(5, 0) { |index| "new_#{index}" } # => ["a", "b", "c", "d", nil]
1716
+ # a = ['a', 'b', 'c', 'd']
1717
+ # a.fill(5, 2) { |index| "new_#{index}" } # => ["a", "b", "c", "d", nil, "new_5", "new_6"]
1718
+ #
1719
+ # If `length` is zero or less, replaces no elements:
1720
+ # a = ['a', 'b', 'c', 'd']
1721
+ # a.fill(1, 0) { |index| "new_#{index}" } # => ["a", "b", "c", "d"]
1722
+ # a.fill(1, -1) { |index| "new_#{index}" } # => ["a", "b", "c", "d"]
1723
+ #
1724
+ # With arguments `obj` and `range`, and a block given, calls the block with each
1725
+ # index in the given range; replaces the corresponding element with the block's
1726
+ # return value.
1727
+ #
1728
+ # If the range is positive and ascending (`range 0 < range.begin <= range.end`,
1729
+ # replaces elements from `range.begin` to `range.end`:
1730
+ # a = ['a', 'b', 'c', 'd']
1731
+ # a.fill(1..1) { |index| "new_#{index}" } # => ["a", "new_1", "c", "d"]
1732
+ #
1733
+ # If `range.first` is negative, does nothing:
1734
+ # a = ['a', 'b', 'c', 'd']
1735
+ # a.fill(-1..1) { |index| fail 'Cannot happen' } # => ["a", "b", "c", "d"]
1736
+ #
1737
+ # If `range.last` is negative, counts from the end:
1738
+ # a = ['a', 'b', 'c', 'd']
1739
+ # a.fill(0..-2) { |index| "new_#{index}" } # => ["new_0", "new_1", "new_2", "d"]
1740
+ # a = ['a', 'b', 'c', 'd']
1741
+ # a.fill(1..-2) { |index| "new_#{index}" } # => ["a", "new_1", "new_2", "d"]
1742
+ #
1743
+ # If `range.first` and `range.last` are both negative, both count from the end:
1744
+ # a = ['a', 'b', 'c', 'd']
1745
+ # a.fill(-1..-1) { |index| "new_#{index}" } # => ["a", "b", "c", "new_3"]
1746
+ # a = ['a', 'b', 'c', 'd']
1747
+ # a.fill(-2..-2) { |index| "new_#{index}" } # => ["a", "b", "new_2", "d"]
912
1748
  #
913
1749
  def fill: (Elem obj) -> self
914
1750
  | (Elem obj, int? start, ?int? length) -> self
@@ -916,299 +1752,592 @@ class Array[unchecked out Elem] < Object
916
1752
  | (?int? start, ?int? length) { (::Integer index) -> Elem } -> self
917
1753
  | (::Range[::Integer] range) { (::Integer index) -> Elem } -> self
918
1754
 
919
- # Returns a new array containing all elements of `ary` for which the given
920
- # `block` returns a true value.
921
- #
922
- # If no block is given, an Enumerator is returned instead.
923
- #
924
- # [1,2,3,4,5].select {|num| num.even? } #=> [2, 4]
925
- #
926
- # a = %w[ a b c d e f ]
927
- # a.select {|v| v =~ /[aeiou]/ } #=> ["a", "e"]
1755
+ # <!-- rdoc-file=array.c -->
1756
+ # Calls the block, if given, with each element of `self`; returns a new Array
1757
+ # containing those elements of `self` for which the block returns a truthy
1758
+ # value:
1759
+ # a = [:foo, 'bar', 2, :bam]
1760
+ # a1 = a.select {|element| element.to_s.start_with?('b') }
1761
+ # a1 # => ["bar", :bam]
928
1762
  #
929
- # See also Enumerable#select.
1763
+ # Returns a new Enumerator if no block given:
1764
+ # a = [:foo, 'bar', 2, :bam]
1765
+ # a.select # => #<Enumerator: [:foo, "bar", 2, :bam]:select>
930
1766
  #
931
1767
  # Array#filter is an alias for Array#select.
932
1768
  #
933
1769
  def filter: () { (Elem item) -> boolish } -> ::Array[Elem]
934
1770
  | () -> ::Enumerator[Elem, ::Array[Elem]]
935
1771
 
936
- # Invokes the given block passing in successive elements from `self`, deleting
937
- # elements for which the block returns a `false` value.
1772
+ # <!-- rdoc-file=array.c -->
1773
+ # Calls the block, if given with each element of `self`; removes from `self`
1774
+ # those elements for which the block returns `false` or `nil`.
938
1775
  #
939
- # The array may not be changed instantly every time the block is called.
1776
+ # Returns `self` if any elements were removed:
1777
+ # a = [:foo, 'bar', 2, :bam]
1778
+ # a.select! {|element| element.to_s.start_with?('b') } # => ["bar", :bam]
940
1779
  #
941
- # If changes were made, it will return `self`, otherwise it returns `nil`.
1780
+ # Returns `nil` if no elements were removed.
942
1781
  #
943
- # If no block is given, an Enumerator is returned instead.
944
- #
945
- # See also Array#keep_if.
1782
+ # Returns a new Enumerator if no block given:
1783
+ # a = [:foo, 'bar', 2, :bam]
1784
+ # a.select! # => #<Enumerator: [:foo, "bar", 2, :bam]:select!>
946
1785
  #
947
1786
  # Array#filter! is an alias for Array#select!.
948
1787
  #
949
1788
  def filter!: () { (Elem item) -> boolish } -> self?
950
1789
  | () -> ::Enumerator[Elem, self?]
951
1790
 
952
- # Returns the *index* of the first object in `ary` such that the object is `==`
953
- # to `obj`.
1791
+ # <!--
1792
+ # rdoc-file=array.c
1793
+ # - array.index(object) -> integer or nil
1794
+ # - array.index {|element| ... } -> integer or nil
1795
+ # - array.index -> new_enumerator
1796
+ # -->
1797
+ # Returns the index of a specified element.
954
1798
  #
955
- # If a block is given instead of an argument, returns the *index* of the first
956
- # object for which the block returns `true`. Returns `nil` if no match is
957
- # found.
1799
+ # When argument `object` is given but no block, returns the index of the first
1800
+ # element `element` for which `object == element`:
1801
+ # a = [:foo, 'bar', 2, 'bar']
1802
+ # a.index('bar') # => 1
958
1803
  #
959
- # See also Array#rindex.
1804
+ # Returns `nil` if no such element found.
960
1805
  #
961
- # An Enumerator is returned if neither a block nor argument is given.
1806
+ # When both argument `object` and a block are given, calls the block with each
1807
+ # successive element; returns the index of the first element for which the block
1808
+ # returns a truthy value:
1809
+ # a = [:foo, 'bar', 2, 'bar']
1810
+ # a.index {|element| element == 'bar' } # => 1
962
1811
  #
963
- # a = [ "a", "b", "c" ]
964
- # a.index("b") #=> 1
965
- # a.index("z") #=> nil
966
- # a.index {|x| x == "b"} #=> 1
1812
+ # Returns `nil` if the block never returns a truthy value.
1813
+ #
1814
+ # When neither an argument nor a block is given, returns a new Enumerator:
1815
+ # a = [:foo, 'bar', 2]
1816
+ # e = a.index
1817
+ # e # => #<Enumerator: [:foo, "bar", 2]:index>
1818
+ # e.each {|element| element == 'bar' } # => 1
1819
+ #
1820
+ # Array#find_index is an alias for Array#index.
1821
+ #
1822
+ # Related: #rindex.
967
1823
  #
968
1824
  def find_index: (untyped obj) -> ::Integer?
969
1825
  | () { (Elem item) -> boolish } -> ::Integer?
970
1826
  | () -> ::Enumerator[Elem, ::Integer?]
971
1827
 
972
- # Returns the first element, or the first `n` elements, of the array. If the
973
- # array is empty, the first form returns `nil`, and the second form returns an
974
- # empty array. See also Array#last for the opposite effect.
1828
+ # <!--
1829
+ # rdoc-file=array.c
1830
+ # - array.first -> object or nil
1831
+ # - array.first(n) -> new_array
1832
+ # -->
1833
+ # Returns elements from `self`; does not modify `self`.
975
1834
  #
976
- # a = [ "q", "r", "s", "t" ]
977
- # a.first #=> "q"
978
- # a.first(2) #=> ["q", "r"]
1835
+ # When no argument is given, returns the first element:
1836
+ # a = [:foo, 'bar', 2]
1837
+ # a.first # => :foo
1838
+ # a # => [:foo, "bar", 2]
979
1839
  #
980
- def first: () -> Elem?
981
- | (int n) -> ::Array[Elem]
982
-
983
- # Returns a new array that is a one-dimensional flattening of `self`
984
- # (recursively).
1840
+ # If `self` is empty, returns `nil`.
985
1841
  #
986
- # That is, for every element that is an array, extract its elements into the new
987
- # array.
1842
+ # When non-negative Integer argument `n` is given, returns the first `n`
1843
+ # elements in a new Array:
1844
+ # a = [:foo, 'bar', 2]
1845
+ # a.first(2) # => [:foo, "bar"]
988
1846
  #
989
- # The optional `level` argument determines the level of recursion to flatten.
1847
+ # If `n >= array.size`, returns all elements:
1848
+ # a = [:foo, 'bar', 2]
1849
+ # a.first(50) # => [:foo, "bar", 2]
990
1850
  #
991
- # s = [ 1, 2, 3 ] #=> [1, 2, 3]
992
- # t = [ 4, 5, 6, [7, 8] ] #=> [4, 5, 6, [7, 8]]
993
- # a = [ s, t, 9, 10 ] #=> [[1, 2, 3], [4, 5, 6, [7, 8]], 9, 10]
994
- # a.flatten #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
995
- # a = [ 1, 2, [3, [4, 5] ] ]
996
- # a.flatten(1) #=> [1, 2, 3, [4, 5]]
1851
+ # If `n == 0` returns an new empty Array:
1852
+ # a = [:foo, 'bar', 2]
1853
+ # a.first(0) # []
997
1854
  #
998
- def flatten: (?int level) -> ::Array[untyped]
999
-
1000
- # Flattens `self` in place.
1855
+ # Related: #last.
1001
1856
  #
1002
- # Returns `nil` if no modifications were made (i.e., the array contains no
1003
- # subarrays.)
1004
- #
1005
- # The optional `level` argument determines the level of recursion to flatten.
1857
+ def first: () -> Elem?
1858
+ | (int n) -> ::Array[Elem]
1859
+
1860
+ # <!--
1861
+ # rdoc-file=array.c
1862
+ # - array.flatten -> new_array
1863
+ # - array.flatten(level) -> new_array
1864
+ # -->
1865
+ # Returns a new Array that is a recursive flattening of `self`:
1866
+ # * Each non-Array element is unchanged.
1867
+ # * Each Array is replaced by its individual elements.
1868
+ #
1869
+ #
1870
+ # With non-negative Integer argument `level`, flattens recursively through
1871
+ # `level` levels:
1872
+ # a = [ 0, [ 1, [2, 3], 4 ], 5 ]
1873
+ # a.flatten(0) # => [0, [1, [2, 3], 4], 5]
1874
+ # a = [ 0, [ 1, [2, 3], 4 ], 5 ]
1875
+ # a.flatten(1) # => [0, 1, [2, 3], 4, 5]
1876
+ # a = [ 0, [ 1, [2, 3], 4 ], 5 ]
1877
+ # a.flatten(2) # => [0, 1, 2, 3, 4, 5]
1878
+ # a = [ 0, [ 1, [2, 3], 4 ], 5 ]
1879
+ # a.flatten(3) # => [0, 1, 2, 3, 4, 5]
1880
+ #
1881
+ # With no argument, a `nil` argument, or with negative argument `level`,
1882
+ # flattens all levels:
1883
+ # a = [ 0, [ 1, [2, 3], 4 ], 5 ]
1884
+ # a.flatten # => [0, 1, 2, 3, 4, 5]
1885
+ # [0, 1, 2].flatten # => [0, 1, 2]
1886
+ # a = [ 0, [ 1, [2, 3], 4 ], 5 ]
1887
+ # a.flatten(-1) # => [0, 1, 2, 3, 4, 5]
1888
+ # a = [ 0, [ 1, [2, 3], 4 ], 5 ]
1889
+ # a.flatten(-2) # => [0, 1, 2, 3, 4, 5]
1890
+ # [0, 1, 2].flatten(-1) # => [0, 1, 2]
1006
1891
  #
1007
- # a = [ 1, 2, [3, [4, 5] ] ]
1008
- # a.flatten! #=> [1, 2, 3, 4, 5]
1009
- # a.flatten! #=> nil
1010
- # a #=> [1, 2, 3, 4, 5]
1011
- # a = [ 1, 2, [3, [4, 5] ] ]
1012
- # a.flatten!(1) #=> [1, 2, 3, [4, 5]]
1892
+ def flatten: (?int level) -> ::Array[untyped]
1893
+
1894
+ # <!--
1895
+ # rdoc-file=array.c
1896
+ # - array.flatten! -> self or nil
1897
+ # - array.flatten!(level) -> self or nil
1898
+ # -->
1899
+ # Replaces each nested Array in `self` with the elements from that Array;
1900
+ # returns `self` if any changes, `nil` otherwise.
1901
+ #
1902
+ # With non-negative Integer argument `level`, flattens recursively through
1903
+ # `level` levels:
1904
+ # a = [ 0, [ 1, [2, 3], 4 ], 5 ]
1905
+ # a.flatten!(1) # => [0, 1, [2, 3], 4, 5]
1906
+ # a = [ 0, [ 1, [2, 3], 4 ], 5 ]
1907
+ # a.flatten!(2) # => [0, 1, 2, 3, 4, 5]
1908
+ # a = [ 0, [ 1, [2, 3], 4 ], 5 ]
1909
+ # a.flatten!(3) # => [0, 1, 2, 3, 4, 5]
1910
+ # [0, 1, 2].flatten!(1) # => nil
1911
+ #
1912
+ # With no argument, a `nil` argument, or with negative argument `level`,
1913
+ # flattens all levels:
1914
+ # a = [ 0, [ 1, [2, 3], 4 ], 5 ]
1915
+ # a.flatten! # => [0, 1, 2, 3, 4, 5]
1916
+ # [0, 1, 2].flatten! # => nil
1917
+ # a = [ 0, [ 1, [2, 3], 4 ], 5 ]
1918
+ # a.flatten!(-1) # => [0, 1, 2, 3, 4, 5]
1919
+ # a = [ 0, [ 1, [2, 3], 4 ], 5 ]
1920
+ # a.flatten!(-2) # => [0, 1, 2, 3, 4, 5]
1921
+ # [0, 1, 2].flatten!(-1) # => nil
1013
1922
  #
1014
1923
  def flatten!: (?int level) -> self?
1015
1924
 
1016
- # Compute a hash-code for this array.
1925
+ # <!--
1926
+ # rdoc-file=array.c
1927
+ # - array.hash -> integer
1928
+ # -->
1929
+ # Returns the integer hash value for `self`.
1017
1930
  #
1018
1931
  # Two arrays with the same content will have the same hash code (and will
1019
- # compare using #eql?).
1020
- #
1021
- # See also Object#hash.
1932
+ # compare using eql?):
1933
+ # [0, 1, 2].hash == [0, 1, 2].hash # => true
1934
+ # [0, 1, 2].hash == [0, 1, 3].hash # => false
1022
1935
  #
1023
1936
  def hash: () -> ::Integer
1024
1937
 
1025
- # Returns `true` if the given `object` is present in `self` (that is, if any
1026
- # element `==` `object`), otherwise returns `false`.
1027
- #
1028
- # a = [ "a", "b", "c" ]
1029
- # a.include?("b") #=> true
1030
- # a.include?("z") #=> false
1938
+ # <!--
1939
+ # rdoc-file=array.c
1940
+ # - array.include?(obj) -> true or false
1941
+ # -->
1942
+ # Returns `true` if for some index `i` in `self`, `obj == self[i]`; otherwise
1943
+ # `false`:
1944
+ # [0, 1, 2].include?(2) # => true
1945
+ # [0, 1, 2].include?(3) # => false
1031
1946
  #
1032
1947
  def include?: (Elem object) -> bool
1033
1948
 
1034
- # Returns the *index* of the first object in `ary` such that the object is `==`
1035
- # to `obj`.
1949
+ # <!-- rdoc-file=array.c -->
1950
+ # Returns the index of a specified element.
1036
1951
  #
1037
- # If a block is given instead of an argument, returns the *index* of the first
1038
- # object for which the block returns `true`. Returns `nil` if no match is
1039
- # found.
1952
+ # When argument `object` is given but no block, returns the index of the first
1953
+ # element `element` for which `object == element`:
1954
+ # a = [:foo, 'bar', 2, 'bar']
1955
+ # a.index('bar') # => 1
1040
1956
  #
1041
- # See also Array#rindex.
1957
+ # Returns `nil` if no such element found.
1042
1958
  #
1043
- # An Enumerator is returned if neither a block nor argument is given.
1959
+ # When both argument `object` and a block are given, calls the block with each
1960
+ # successive element; returns the index of the first element for which the block
1961
+ # returns a truthy value:
1962
+ # a = [:foo, 'bar', 2, 'bar']
1963
+ # a.index {|element| element == 'bar' } # => 1
1044
1964
  #
1045
- # a = [ "a", "b", "c" ]
1046
- # a.index("b") #=> 1
1047
- # a.index("z") #=> nil
1048
- # a.index {|x| x == "b"} #=> 1
1965
+ # Returns `nil` if the block never returns a truthy value.
1966
+ #
1967
+ # When neither an argument nor a block is given, returns a new Enumerator:
1968
+ # a = [:foo, 'bar', 2]
1969
+ # e = a.index
1970
+ # e # => #<Enumerator: [:foo, "bar", 2]:index>
1971
+ # e.each {|element| element == 'bar' } # => 1
1972
+ #
1973
+ # Array#find_index is an alias for Array#index.
1974
+ #
1975
+ # Related: #rindex.
1049
1976
  #
1050
1977
  alias index find_index
1051
1978
 
1052
- # Inserts the given values before the element with the given `index`.
1053
- #
1054
- # Negative indices count backwards from the end of the array, where `-1` is the
1055
- # last element. If a negative index is used, the given values will be inserted
1056
- # after that element, so using an index of `-1` will insert the values at the
1057
- # end of the array.
1979
+ # <!--
1980
+ # rdoc-file=array.c
1981
+ # - array.insert(index, *objects) -> self
1982
+ # -->
1983
+ # Inserts given `objects` before or after the element at Integer index `offset`;
1984
+ # returns `self`.
1058
1985
  #
1059
- # a = %w{ a b c d }
1060
- # a.insert(2, 99) #=> ["a", "b", 99, "c", "d"]
1061
- # a.insert(-2, 1, 2, 3) #=> ["a", "b", 99, "c", 1, 2, 3, "d"]
1986
+ # When `index` is non-negative, inserts all given `objects` before the element
1987
+ # at offset `index`:
1988
+ # a = [:foo, 'bar', 2]
1989
+ # a.insert(1, :bat, :bam) # => [:foo, :bat, :bam, "bar", 2]
1990
+ #
1991
+ # Extends the array if `index` is beyond the array (`index >= self.size`):
1992
+ # a = [:foo, 'bar', 2]
1993
+ # a.insert(5, :bat, :bam)
1994
+ # a # => [:foo, "bar", 2, nil, nil, :bat, :bam]
1995
+ #
1996
+ # Does nothing if no objects given:
1997
+ # a = [:foo, 'bar', 2]
1998
+ # a.insert(1)
1999
+ # a.insert(50)
2000
+ # a.insert(-50)
2001
+ # a # => [:foo, "bar", 2]
2002
+ #
2003
+ # When `index` is negative, inserts all given `objects` *after* the element at
2004
+ # offset `index+self.size`:
2005
+ # a = [:foo, 'bar', 2]
2006
+ # a.insert(-2, :bat, :bam)
2007
+ # a # => [:foo, "bar", :bat, :bam, 2]
1062
2008
  #
1063
2009
  def insert: (int index, *Elem obj) -> self
1064
2010
 
1065
- # Creates a string representation of `self`, by calling #inspect on each
1066
- # element.
2011
+ # <!--
2012
+ # rdoc-file=array.c
2013
+ # - array.inspect -> new_string
2014
+ # -->
2015
+ # Returns the new String formed by calling method `#inspect` on each array
2016
+ # element:
2017
+ # a = [:foo, 'bar', 2]
2018
+ # a.inspect # => "[:foo, \"bar\", 2]"
1067
2019
  #
1068
- # [ "a", "b", "c" ].to_s #=> "[\"a\", \"b\", \"c\"]"
2020
+ # Array#to_s is an alias for Array#inspect.
1069
2021
  #
1070
2022
  def inspect: () -> String
1071
2023
 
1072
- # Set Intersection --- Returns a new array containing unique elements common to
1073
- # `self` and `other_ary`s. Order is preserved from the original array.
2024
+ # <!--
2025
+ # rdoc-file=array.c
2026
+ # - ary.intersect?(other_ary) -> true or false
2027
+ # -->
2028
+ # Returns `true` if the array and `other_ary` have at least one element in
2029
+ # common, otherwise returns `false`.
2030
+ #
2031
+ # a = [ 1, 2, 3 ]
2032
+ # b = [ 3, 4, 5 ]
2033
+ # c = [ 5, 6, 7 ]
2034
+ # a.intersect?(b) #=> true
2035
+ # a.intersect?(c) #=> false
2036
+ #
2037
+ def intersect?: (_ToAry[untyped]) -> bool
2038
+
2039
+ # <!--
2040
+ # rdoc-file=array.c
2041
+ # - array.intersection(*other_arrays) -> new_array
2042
+ # -->
2043
+ # Returns a new Array containing each element found both in `self` and in all of
2044
+ # the given Arrays `other_arrays`; duplicates are omitted; items are compared
2045
+ # using `eql?`:
2046
+ # [0, 1, 2, 3].intersection([0, 1, 2], [0, 1, 3]) # => [0, 1]
2047
+ # [0, 0, 1, 1, 2, 3].intersection([0, 1, 2], [0, 1, 3]) # => [0, 1]
1074
2048
  #
1075
- # It compares elements using their #hash and #eql? methods for efficiency.
2049
+ # Preserves order from `self`:
2050
+ # [0, 1, 2].intersection([2, 1, 0]) # => [0, 1, 2]
1076
2051
  #
1077
- # [ 1, 1, 3, 5 ].intersection([ 3, 2, 1 ]) # => [ 1, 3 ]
1078
- # [ "a", "b", "z" ].intersection([ "a", "b", "c" ], [ "b" ]) # => [ "b" ]
1079
- # [ "a" ].intersection #=> [ "a" ]
2052
+ # Returns a copy of `self` if no arguments given.
1080
2053
  #
1081
- # See also Array#&.
2054
+ # Related: Array#&.
1082
2055
  #
1083
2056
  def intersection: (*::Array[untyped] | _ToAry[untyped] other_ary) -> ::Array[Elem]
1084
2057
 
1085
- # Returns a string created by converting each element of the array to a string,
1086
- # separated by the given `separator`. If the `separator` is `nil`, it uses
1087
- # current `$,`. If both the `separator` and `$,` are `nil`, it uses an empty
1088
- # string.
2058
+ # <!--
2059
+ # rdoc-file=array.c
2060
+ # - array.join ->new_string
2061
+ # - array.join(separator = $,) -> new_string
2062
+ # -->
2063
+ # Returns the new String formed by joining the array elements after conversion.
2064
+ # For each element `element`
2065
+ # * Uses `element.to_s` if `element` is not a `kind_of?(Array)`.
2066
+ # * Uses recursive `element.join(separator)` if `element` is a
2067
+ # `kind_of?(Array)`.
1089
2068
  #
1090
- # [ "a", "b", "c" ].join #=> "abc"
1091
- # [ "a", "b", "c" ].join("-") #=> "a-b-c"
1092
2069
  #
1093
- # For nested arrays, join is applied recursively:
2070
+ # With no argument, joins using the output field separator, `$,`:
2071
+ # a = [:foo, 'bar', 2]
2072
+ # $, # => nil
2073
+ # a.join # => "foobar2"
1094
2074
  #
1095
- # [ "a", [1, 2, [:x, :y]], "b" ].join("-") #=> "a-1-2-x-y-b"
2075
+ # With string argument `separator`, joins using that separator:
2076
+ # a = [:foo, 'bar', 2]
2077
+ # a.join("\n") # => "foo\nbar\n2"
2078
+ #
2079
+ # Joins recursively for nested Arrays:
2080
+ # a = [:foo, [:bar, [:baz, :bat]]]
2081
+ # a.join # => "foobarbazbat"
1096
2082
  #
1097
2083
  def join: (?string separator) -> String
1098
2084
 
1099
- # Deletes every element of `self` for which the given block evaluates to
1100
- # `false`, and returns `self`.
1101
- #
1102
- # If no block is given, an Enumerator is returned instead.
1103
- #
1104
- # a = %w[ a b c d e f ]
1105
- # a.keep_if {|v| v =~ /[aeiou]/ } #=> ["a", "e"]
1106
- # a #=> ["a", "e"]
2085
+ # <!--
2086
+ # rdoc-file=array.c
2087
+ # - array.keep_if {|element| ... } -> self
2088
+ # - array.keep_if -> new_enumeration
2089
+ # -->
2090
+ # Retains those elements for which the block returns a truthy value; deletes all
2091
+ # other elements; returns `self`:
2092
+ # a = [:foo, 'bar', 2, :bam]
2093
+ # a.keep_if {|element| element.to_s.start_with?('b') } # => ["bar", :bam]
1107
2094
  #
1108
- # See also Array#select!.
2095
+ # Returns a new Enumerator if no block given:
2096
+ # a = [:foo, 'bar', 2, :bam]
2097
+ # a.keep_if # => #<Enumerator: [:foo, "bar", 2, :bam]:keep_if>
1109
2098
  #
1110
2099
  def keep_if: () { (Elem item) -> boolish } -> self
1111
2100
  | () -> ::Enumerator[Elem, self]
1112
2101
 
1113
- # Returns the last element(s) of `self`. If the array is empty, the first form
1114
- # returns `nil`.
2102
+ # <!--
2103
+ # rdoc-file=array.c
2104
+ # - array.last -> object or nil
2105
+ # - array.last(n) -> new_array
2106
+ # -->
2107
+ # Returns elements from `self`; `self` is not modified.
2108
+ #
2109
+ # When no argument is given, returns the last element:
2110
+ # a = [:foo, 'bar', 2]
2111
+ # a.last # => 2
2112
+ # a # => [:foo, "bar", 2]
2113
+ #
2114
+ # If `self` is empty, returns `nil`.
1115
2115
  #
1116
- # See also Array#first for the opposite effect.
2116
+ # When non-negative Innteger argument `n` is given, returns the last `n`
2117
+ # elements in a new Array:
2118
+ # a = [:foo, 'bar', 2]
2119
+ # a.last(2) # => ["bar", 2]
1117
2120
  #
1118
- # a = [ "w", "x", "y", "z" ]
1119
- # a.last #=> "z"
1120
- # a.last(2) #=> ["y", "z"]
2121
+ # If `n >= array.size`, returns all elements:
2122
+ # a = [:foo, 'bar', 2]
2123
+ # a.last(50) # => [:foo, "bar", 2]
2124
+ #
2125
+ # If `n == 0`, returns an new empty Array:
2126
+ # a = [:foo, 'bar', 2]
2127
+ # a.last(0) # []
2128
+ #
2129
+ # Related: #first.
1121
2130
  #
1122
2131
  def last: () -> Elem?
1123
2132
  | (int n) -> ::Array[Elem]
1124
2133
 
1125
- # Returns the number of elements in `self`. May be zero.
1126
- #
1127
- # [ 1, 2, 3, 4, 5 ].length #=> 5
1128
- # [].length #=> 0
2134
+ # <!--
2135
+ # rdoc-file=array.c
2136
+ # - array.length -> an_integer
2137
+ # -->
2138
+ # Returns the count of elements in `self`.
1129
2139
  #
1130
2140
  def length: () -> ::Integer
1131
2141
 
1132
- # Invokes the given block once for each element of `self`.
1133
- #
1134
- # Creates a new array containing the values returned by the block.
2142
+ # <!-- rdoc-file=array.c -->
2143
+ # Calls the block, if given, with each element of `self`; returns a new Array
2144
+ # whose elements are the return values from the block:
2145
+ # a = [:foo, 'bar', 2]
2146
+ # a1 = a.map {|element| element.class }
2147
+ # a1 # => [Symbol, String, Integer]
1135
2148
  #
1136
- # See also Enumerable#collect.
2149
+ # Returns a new Enumerator if no block given:
2150
+ # a = [:foo, 'bar', 2]
2151
+ # a1 = a.map
2152
+ # a1 # => #<Enumerator: [:foo, "bar", 2]:map>
1137
2153
  #
1138
- # If no block is given, an Enumerator is returned instead.
1139
- #
1140
- # a = [ "a", "b", "c", "d" ]
1141
- # a.collect {|x| x + "!"} #=> ["a!", "b!", "c!", "d!"]
1142
- # a.map.with_index {|x, i| x * i} #=> ["", "b", "cc", "ddd"]
1143
- # a #=> ["a", "b", "c", "d"]
2154
+ # Array#collect is an alias for Array#map.
1144
2155
  #
1145
2156
  alias map collect
1146
2157
 
1147
- # Invokes the given block once for each element of `self`, replacing the element
1148
- # with the value returned by the block.
1149
- #
1150
- # See also Enumerable#collect.
2158
+ # <!-- rdoc-file=array.c -->
2159
+ # Calls the block, if given, with each element; replaces the element with the
2160
+ # block's return value:
2161
+ # a = [:foo, 'bar', 2]
2162
+ # a.map! { |element| element.class } # => [Symbol, String, Integer]
1151
2163
  #
1152
- # If no block is given, an Enumerator is returned instead.
2164
+ # Returns a new Enumerator if no block given:
2165
+ # a = [:foo, 'bar', 2]
2166
+ # a1 = a.map!
2167
+ # a1 # => #<Enumerator: [:foo, "bar", 2]:map!>
1153
2168
  #
1154
- # a = [ "a", "b", "c", "d" ]
1155
- # a.map! {|x| x + "!" }
1156
- # a #=> [ "a!", "b!", "c!", "d!" ]
1157
- # a.collect!.with_index {|x, i| x[0...i] }
1158
- # a #=> ["", "b", "c!", "d!"]
2169
+ # Array#collect! is an alias for Array#map!.
1159
2170
  #
1160
2171
  alias map! collect!
1161
2172
 
1162
- # Returns the object in *ary* with the maximum value. The first form assumes all
1163
- # objects implement Comparable; the second uses the block to return *a <=> b*.
2173
+ # <!--
2174
+ # rdoc-file=array.c
2175
+ # - array.max -> element
2176
+ # - array.max {|a, b| ... } -> element
2177
+ # - array.max(n) -> new_array
2178
+ # - array.max(n) {|a, b| ... } -> new_array
2179
+ # -->
2180
+ # Returns one of the following:
2181
+ # * The maximum-valued element from `self`.
2182
+ # * A new Array of maximum-valued elements selected from `self`.
2183
+ #
2184
+ #
2185
+ # When no block is given, each element in `self` must respond to method `<=>`
2186
+ # with an Integer.
2187
+ #
2188
+ # With no argument and no block, returns the element in `self` having the
2189
+ # maximum value per method `<=>`:
2190
+ # [0, 1, 2].max # => 2
1164
2191
  #
1165
- # ary = %w(albatross dog horse)
1166
- # ary.max #=> "horse"
1167
- # ary.max {|a, b| a.length <=> b.length} #=> "albatross"
2192
+ # With an argument Integer `n` and no block, returns a new Array with at most
2193
+ # `n` elements, in descending order per method `<=>`:
2194
+ # [0, 1, 2, 3].max(3) # => [3, 2, 1]
2195
+ # [0, 1, 2, 3].max(6) # => [3, 2, 1, 0]
1168
2196
  #
1169
- # If the `n` argument is given, maximum `n` elements are returned as an array.
2197
+ # When a block is given, the block must return an Integer.
1170
2198
  #
1171
- # ary = %w[albatross dog horse]
1172
- # ary.max(2) #=> ["horse", "dog"]
1173
- # ary.max(2) {|a, b| a.length <=> b.length } #=> ["albatross", "horse"]
2199
+ # With a block and no argument, calls the block `self.size-1` times to compare
2200
+ # elements; returns the element having the maximum value per the block:
2201
+ # ['0', '00', '000'].max {|a, b| a.size <=> b.size } # => "000"
2202
+ #
2203
+ # With an argument `n` and a block, returns a new Array with at most `n`
2204
+ # elements, in descending order per the block:
2205
+ # ['0', '00', '000'].max(2) {|a, b| a.size <=> b.size } # => ["000", "00"]
1174
2206
  #
1175
2207
  def max: () -> Elem?
1176
2208
  | () { (Elem a, Elem b) -> ::Integer? } -> Elem?
1177
2209
  | (int n) -> ::Array[Elem]
1178
2210
  | (int n) { (Elem a, Elem b) -> ::Integer? } -> ::Array[Elem]
1179
2211
 
1180
- # Returns the object in *ary* with the minimum value. The first form assumes all
1181
- # objects implement Comparable; the second uses the block to return *a <=> b*.
2212
+ # <!--
2213
+ # rdoc-file=array.c
2214
+ # - array.min -> element
2215
+ # - array.min { |a, b| ... } -> element
2216
+ # - array.min(n) -> new_array
2217
+ # - array.min(n) { |a, b| ... } -> new_array
2218
+ # -->
2219
+ # Returns one of the following:
2220
+ # * The minimum-valued element from `self`.
2221
+ # * A new Array of minimum-valued elements selected from `self`.
2222
+ #
2223
+ #
2224
+ # When no block is given, each element in `self` must respond to method `<=>`
2225
+ # with an Integer.
2226
+ #
2227
+ # With no argument and no block, returns the element in `self` having the
2228
+ # minimum value per method `<=>`:
2229
+ # [0, 1, 2].min # => 0
2230
+ #
2231
+ # With Integer argument `n` and no block, returns a new Array with at most `n`
2232
+ # elements, in ascending order per method `<=>`:
2233
+ # [0, 1, 2, 3].min(3) # => [0, 1, 2]
2234
+ # [0, 1, 2, 3].min(6) # => [0, 1, 2, 3]
1182
2235
  #
1183
- # ary = %w(albatross dog horse)
1184
- # ary.min #=> "albatross"
1185
- # ary.min {|a, b| a.length <=> b.length} #=> "dog"
2236
+ # When a block is given, the block must return an Integer.
1186
2237
  #
1187
- # If the `n` argument is given, minimum `n` elements are returned as an array.
2238
+ # With a block and no argument, calls the block `self.size-1` times to compare
2239
+ # elements; returns the element having the minimum value per the block:
2240
+ # ['0', '00', '000'].min { |a, b| a.size <=> b.size } # => "0"
1188
2241
  #
1189
- # ary = %w[albatross dog horse]
1190
- # ary.min(2) #=> ["albatross", "dog"]
1191
- # ary.min(2) {|a, b| a.length <=> b.length } #=> ["dog", "horse"]
2242
+ # With an argument `n` and a block, returns a new Array with at most `n`
2243
+ # elements, in ascending order per the block:
2244
+ # ['0', '00', '000'].min(2) {|a, b| a.size <=> b.size } # => ["0", "00"]
1192
2245
  #
1193
2246
  alias min max
1194
2247
 
1195
- # Returns a two element array which contains the minimum and the maximum value
1196
- # in the array.
2248
+ # <!--
2249
+ # rdoc-file=array.c
2250
+ # - array.minmax -> [min_val, max_val]
2251
+ # - array.minmax {|a, b| ... } -> [min_val, max_val]
2252
+ # -->
2253
+ # Returns a new 2-element Array containing the minimum and maximum values from
2254
+ # `self`, either per method `<=>` or per a given block:.
1197
2255
  #
1198
- # Can be given an optional block to override the default comparison method `a
1199
- # <=> b`.
2256
+ # When no block is given, each element in `self` must respond to method `<=>`
2257
+ # with an Integer; returns a new 2-element Array containing the minimum and
2258
+ # maximum values from `self`, per method `<=>`:
2259
+ # [0, 1, 2].minmax # => [0, 2]
2260
+ #
2261
+ # When a block is given, the block must return an Integer; the block is called
2262
+ # `self.size-1` times to compare elements; returns a new 2-element Array
2263
+ # containing the minimum and maximum values from `self`, per the block:
2264
+ # ['0', '00', '000'].minmax {|a, b| a.size <=> b.size } # => ["0", "000"]
1200
2265
  #
1201
2266
  def minmax: () -> [ Elem?, Elem? ]
1202
2267
  | () { (Elem a, Elem b) -> ::Integer? } -> [ Elem?, Elem? ]
1203
2268
 
1204
- # See also Enumerable#none?
2269
+ # <!--
2270
+ # rdoc-file=array.c
2271
+ # - array.none? -> true or false
2272
+ # - array.none? {|element| ... } -> true or false
2273
+ # - array.none?(obj) -> true or false
2274
+ # -->
2275
+ # Returns `true` if no element of `self` meet a given criterion.
2276
+ #
2277
+ # With no block given and no argument, returns `true` if `self` has no truthy
2278
+ # elements, `false` otherwise:
2279
+ # [nil, false].none? # => true
2280
+ # [nil, 0, false].none? # => false
2281
+ # [].none? # => true
2282
+ #
2283
+ # With a block given and no argument, calls the block with each element in
2284
+ # `self`; returns `true` if the block returns no truthy value, `false`
2285
+ # otherwise:
2286
+ # [0, 1, 2].none? {|element| element > 3 } # => true
2287
+ # [0, 1, 2].none? {|element| element > 1 } # => false
2288
+ #
2289
+ # If argument `obj` is given, returns `true` if `obj.===` no element, `false`
2290
+ # otherwise:
2291
+ # ['food', 'drink'].none?(/bar/) # => true
2292
+ # ['food', 'drink'].none?(/foo/) # => false
2293
+ # [].none?(/foo/) # => true
2294
+ # [0, 1, 2].none?(3) # => true
2295
+ # [0, 1, 2].none?(1) # => false
2296
+ #
2297
+ # Related: Enumerable#none?
1205
2298
  #
1206
2299
  alias none? all?
1207
2300
 
1208
- # See also Enumerable#one?
2301
+ # <!--
2302
+ # rdoc-file=array.c
2303
+ # - array.one? -> true or false
2304
+ # - array.one? {|element| ... } -> true or false
2305
+ # - array.one?(obj) -> true or false
2306
+ # -->
2307
+ # Returns `true` if exactly one element of `self` meets a given criterion.
2308
+ #
2309
+ # With no block given and no argument, returns `true` if `self` has exactly one
2310
+ # truthy element, `false` otherwise:
2311
+ # [nil, 0].one? # => true
2312
+ # [0, 0].one? # => false
2313
+ # [nil, nil].one? # => false
2314
+ # [].one? # => false
2315
+ #
2316
+ # With a block given and no argument, calls the block with each element in
2317
+ # `self`; returns `true` if the block a truthy value for exactly one element,
2318
+ # `false` otherwise:
2319
+ # [0, 1, 2].one? {|element| element > 0 } # => false
2320
+ # [0, 1, 2].one? {|element| element > 1 } # => true
2321
+ # [0, 1, 2].one? {|element| element > 2 } # => false
2322
+ #
2323
+ # If argument `obj` is given, returns `true` if `obj.===` exactly one element,
2324
+ # `false` otherwise:
2325
+ # [0, 1, 2].one?(0) # => true
2326
+ # [0, 0, 1].one?(0) # => false
2327
+ # [1, 1, 2].one?(0) # => false
2328
+ # ['food', 'drink'].one?(/bar/) # => false
2329
+ # ['food', 'drink'].one?(/foo/) # => true
2330
+ # [].one?(/foo/) # => false
2331
+ #
2332
+ # Related: Enumerable#one?
1209
2333
  #
1210
2334
  alias one? none?
1211
2335
 
2336
+ # <!--
2337
+ # rdoc-file=pack.rb
2338
+ # - arr.pack( aTemplateString ) -> aBinaryString
2339
+ # - arr.pack( aTemplateString, buffer: aBufferString ) -> aBufferString
2340
+ # -->
1212
2341
  # Packs the contents of *arr* into a binary sequence according to the directives
1213
2342
  # in *aTemplateString* (see the table below) Directives ``A,'' ``a,'' and ``Z''
1214
2343
  # may be followed by a count, which gives the width of the resulting field. The
@@ -1233,6 +2362,16 @@ class Array[unchecked out Elem] < Object
1233
2362
  # *offsetOfBuffer* are overwritten by the result. If it's shorter, the gap is
1234
2363
  # filled with ```\0`''.
1235
2364
  #
2365
+ # # packed data is appended by default
2366
+ # [255].pack("C", buffer:"foo".b) #=> "foo\xFF"
2367
+ #
2368
+ # # "@0" (offset 0) specifies that packed data is filled from beginning.
2369
+ # # Also, original data after packed data is removed. ("oo" is removed.)
2370
+ # [255].pack("@0C", buffer:"foo".b) #=> "\xFF"
2371
+ #
2372
+ # # If the offset is bigger than the original length, \x00 is filled.
2373
+ # [255].pack("@5C", buffer:"foo".b) #=> "foo\x00\x00\xFF"
2374
+ #
1236
2375
  # Note that ``buffer:'' option does not guarantee not to allocate memory in
1237
2376
  # `pack`. If the capacity of *aBufferString* is not enough, `pack` allocates
1238
2377
  # memory.
@@ -1277,14 +2416,14 @@ class Array[unchecked out Elem] < Object
1277
2416
  # S> s> S!> s!> | Integer | same as the directives without ">" except
1278
2417
  # L> l> L!> l!> | | big endian
1279
2418
  # I!> i!> | | (available since Ruby 1.9.3)
1280
- # Q> q> Q!> q!> | | "S>" is same as "n"
1281
- # J> j> J!> j!> | | "L>" is same as "N"
2419
+ # Q> q> Q!> q!> | | "S>" is the same as "n"
2420
+ # J> j> J!> j!> | | "L>" is the same as "N"
1282
2421
  # | |
1283
2422
  # S< s< S!< s!< | Integer | same as the directives without "<" except
1284
2423
  # L< l< L!< l!< | | little endian
1285
2424
  # I!< i!< | | (available since Ruby 1.9.3)
1286
- # Q< q< Q!< q!< | | "S<" is same as "v"
1287
- # J< j< J!< j!< | | "L<" is same as "V"
2425
+ # Q< q< Q!< q!< | | "S<" is the same as "v"
2426
+ # J< j< J!< j!< | | "L<" is the same as "V"
1288
2427
  # | |
1289
2428
  # n | Integer | 16-bit unsigned, network (big-endian) byte order
1290
2429
  # N | Integer | 32-bit unsigned, network (big-endian) byte order
@@ -1333,659 +2472,1300 @@ class Array[unchecked out Elem] < Object
1333
2472
  #
1334
2473
  def pack: (string fmt, ?buffer: String?) -> String
1335
2474
 
1336
- # When invoked with a block, yield all permutations of length `n` of the
1337
- # elements of the array, then return the array itself.
1338
- #
1339
- # If `n` is not specified, yield all permutations of all elements.
1340
- #
1341
- # The implementation makes no guarantees about the order in which the
1342
- # permutations are yielded.
1343
- #
1344
- # If no block is given, an Enumerator is returned instead.
1345
- #
1346
- # Examples:
2475
+ # <!--
2476
+ # rdoc-file=array.c
2477
+ # - array.permutation {|element| ... } -> self
2478
+ # - array.permutation(n) {|element| ... } -> self
2479
+ # - array.permutation -> new_enumerator
2480
+ # - array.permutation(n) -> new_enumerator
2481
+ # -->
2482
+ # When invoked with a block, yield all permutations of elements of `self`;
2483
+ # returns `self`. The order of permutations is indeterminate.
2484
+ #
2485
+ # When a block and an in-range positive Integer argument `n` (`0 < n <=
2486
+ # self.size`) are given, calls the block with all `n`-tuple permutations of
2487
+ # `self`.
1347
2488
  #
1348
- # a = [1, 2, 3]
1349
- # a.permutation.to_a #=> [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
1350
- # a.permutation(1).to_a #=> [[1],[2],[3]]
1351
- # a.permutation(2).to_a #=> [[1,2],[1,3],[2,1],[2,3],[3,1],[3,2]]
1352
- # a.permutation(3).to_a #=> [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
1353
- # a.permutation(0).to_a #=> [[]] # one permutation of length 0
1354
- # a.permutation(4).to_a #=> [] # no permutations of length 4
2489
+ # Example:
2490
+ # a = [0, 1, 2]
2491
+ # a.permutation(2) {|permutation| p permutation }
2492
+ #
2493
+ # Output:
2494
+ # [0, 1]
2495
+ # [0, 2]
2496
+ # [1, 0]
2497
+ # [1, 2]
2498
+ # [2, 0]
2499
+ # [2, 1]
2500
+ #
2501
+ # Another example:
2502
+ # a = [0, 1, 2]
2503
+ # a.permutation(3) {|permutation| p permutation }
2504
+ #
2505
+ # Output:
2506
+ # [0, 1, 2]
2507
+ # [0, 2, 1]
2508
+ # [1, 0, 2]
2509
+ # [1, 2, 0]
2510
+ # [2, 0, 1]
2511
+ # [2, 1, 0]
2512
+ #
2513
+ # When `n` is zero, calls the block once with a new empty Array:
2514
+ # a = [0, 1, 2]
2515
+ # a.permutation(0) {|permutation| p permutation }
2516
+ #
2517
+ # Output:
2518
+ # []
2519
+ #
2520
+ # When `n` is out of range (negative or larger than `self.size`), does not call
2521
+ # the block:
2522
+ # a = [0, 1, 2]
2523
+ # a.permutation(-1) {|permutation| fail 'Cannot happen' }
2524
+ # a.permutation(4) {|permutation| fail 'Cannot happen' }
2525
+ #
2526
+ # When a block given but no argument, behaves the same as
2527
+ # `a.permutation(a.size)`:
2528
+ # a = [0, 1, 2]
2529
+ # a.permutation {|permutation| p permutation }
2530
+ #
2531
+ # Output:
2532
+ # [0, 1, 2]
2533
+ # [0, 2, 1]
2534
+ # [1, 0, 2]
2535
+ # [1, 2, 0]
2536
+ # [2, 0, 1]
2537
+ # [2, 1, 0]
2538
+ #
2539
+ # Returns a new Enumerator if no block given:
2540
+ # a = [0, 1, 2]
2541
+ # a.permutation # => #<Enumerator: [0, 1, 2]:permutation>
2542
+ # a.permutation(2) # => #<Enumerator: [0, 1, 2]:permutation(2)>
1355
2543
  #
1356
2544
  def permutation: (?int n) -> ::Enumerator[::Array[Elem], ::Array[Elem]]
1357
2545
  | (?int n) { (::Array[Elem] p) -> void } -> ::Array[Elem]
1358
2546
 
1359
- # Removes the last element from `self` and returns it, or `nil` if the array is
1360
- # empty.
2547
+ # <!--
2548
+ # rdoc-file=array.c
2549
+ # - array.pop -> object or nil
2550
+ # - array.pop(n) -> new_array
2551
+ # -->
2552
+ # Removes and returns trailing elements.
2553
+ #
2554
+ # When no argument is given and `self` is not empty, removes and returns the
2555
+ # last element:
2556
+ # a = [:foo, 'bar', 2]
2557
+ # a.pop # => 2
2558
+ # a # => [:foo, "bar"]
2559
+ #
2560
+ # Returns `nil` if the array is empty.
1361
2561
  #
1362
- # If a number `n` is given, returns an array of the last `n` elements (or less)
1363
- # just like `array.slice!(-n, n)` does. See also Array#push for the opposite
1364
- # effect.
2562
+ # When a non-negative Integer argument `n` is given and is in range, removes and
2563
+ # returns the last `n` elements in a new Array:
2564
+ # a = [:foo, 'bar', 2]
2565
+ # a.pop(2) # => ["bar", 2]
1365
2566
  #
1366
- # a = [ "a", "b", "c", "d" ]
1367
- # a.pop #=> "d"
1368
- # a.pop(2) #=> ["b", "c"]
1369
- # a #=> ["a"]
2567
+ # If `n` is positive and out of range, removes and returns all elements:
2568
+ # a = [:foo, 'bar', 2]
2569
+ # a.pop(50) # => [:foo, "bar", 2]
2570
+ #
2571
+ # Related: #push, #shift, #unshift.
1370
2572
  #
1371
2573
  def pop: () -> Elem?
1372
2574
  | (int n) -> ::Array[Elem]
1373
2575
 
1374
- alias prepend unshift
1375
-
1376
- # Returns an array of all combinations of elements from all arrays.
1377
- #
1378
- # The length of the returned array is the product of the length of `self` and
1379
- # the argument arrays.
2576
+ # <!-- rdoc-file=array.c -->
2577
+ # Prepends the given `objects` to `self`:
2578
+ # a = [:foo, 'bar', 2]
2579
+ # a.unshift(:bam, :bat) # => [:bam, :bat, :foo, "bar", 2]
1380
2580
  #
1381
- # If given a block, #product will yield all combinations and return `self`
1382
- # instead.
2581
+ # Array#prepend is an alias for Array#unshift.
1383
2582
  #
1384
- # [1,2,3].product([4,5]) #=> [[1,4],[1,5],[2,4],[2,5],[3,4],[3,5]]
1385
- # [1,2].product([1,2]) #=> [[1,1],[1,2],[2,1],[2,2]]
1386
- # [1,2].product([3,4],[5,6]) #=> [[1,3,5],[1,3,6],[1,4,5],[1,4,6],
1387
- # # [2,3,5],[2,3,6],[2,4,5],[2,4,6]]
1388
- # [1,2].product() #=> [[1],[2]]
1389
- # [1,2].product([]) #=> []
2583
+ # Related: #push, #pop, #shift.
1390
2584
  #
1391
- def product: () -> ::Array[[Elem]]
1392
- | [X] (::Array[X] other_ary) -> ::Array[[Elem, X]]
1393
- | [X, Y] (::Array[X] other_ary1, ::Array[Y] other_ary2) -> ::Array[[Elem, X, Y]]
2585
+ alias prepend unshift
2586
+
2587
+ # <!--
2588
+ # rdoc-file=array.c
2589
+ # - array.product(*other_arrays) -> new_array
2590
+ # - array.product(*other_arrays) {|combination| ... } -> self
2591
+ # -->
2592
+ # Computes and returns or yields all combinations of elements from all the
2593
+ # Arrays, including both `self` and `other_arrays`.
2594
+ # * The number of combinations is the product of the sizes of all the arrays,
2595
+ # including both `self` and `other_arrays`.
2596
+ # * The order of the returned combinations is indeterminate.
2597
+ #
2598
+ #
2599
+ # When no block is given, returns the combinations as an Array of Arrays:
2600
+ # a = [0, 1, 2]
2601
+ # a1 = [3, 4]
2602
+ # a2 = [5, 6]
2603
+ # p = a.product(a1)
2604
+ # p.size # => 6 # a.size * a1.size
2605
+ # p # => [[0, 3], [0, 4], [1, 3], [1, 4], [2, 3], [2, 4]]
2606
+ # p = a.product(a1, a2)
2607
+ # p.size # => 12 # a.size * a1.size * a2.size
2608
+ # p # => [[0, 3, 5], [0, 3, 6], [0, 4, 5], [0, 4, 6], [1, 3, 5], [1, 3, 6], [1, 4, 5], [1, 4, 6], [2, 3, 5], [2, 3, 6], [2, 4, 5], [2, 4, 6]]
2609
+ #
2610
+ # If any argument is an empty Array, returns an empty Array.
2611
+ #
2612
+ # If no argument is given, returns an Array of 1-element Arrays, each containing
2613
+ # an element of `self`:
2614
+ # a.product # => [[0], [1], [2]]
2615
+ #
2616
+ # When a block is given, yields each combination as an Array; returns `self`:
2617
+ # a.product(a1) {|combination| p combination }
2618
+ #
2619
+ # Output:
2620
+ # [0, 3]
2621
+ # [0, 4]
2622
+ # [1, 3]
2623
+ # [1, 4]
2624
+ # [2, 3]
2625
+ # [2, 4]
2626
+ #
2627
+ # If any argument is an empty Array, does not call the block:
2628
+ # a.product(a1, a2, []) {|combination| fail 'Cannot happen' }
2629
+ #
2630
+ # If no argument is given, yields each element of `self` as a 1-element Array:
2631
+ # a.product {|combination| p combination }
2632
+ #
2633
+ # Output:
2634
+ # [0]
2635
+ # [1]
2636
+ # [2]
2637
+ #
2638
+ def product: () -> ::Array[[ Elem ]]
2639
+ | [X] (::Array[X] other_ary) -> ::Array[[ Elem, X ]]
2640
+ | [X, Y] (::Array[X] other_ary1, ::Array[Y] other_ary2) -> ::Array[[ Elem, X, Y ]]
1394
2641
  | [U] (*::Array[U] other_arys) -> ::Array[::Array[Elem | U]]
1395
2642
 
1396
- # Append --- Pushes the given object(s) on to the end of this array. This
1397
- # expression returns the array itself, so several appends may be chained
1398
- # together. See also Array#pop for the opposite effect.
2643
+ # <!--
2644
+ # rdoc-file=array.c
2645
+ # - array.push(*objects) -> self
2646
+ # -->
2647
+ # Appends trailing elements.
1399
2648
  #
1400
- # a = [ "a", "b", "c" ]
1401
- # a.push("d", "e", "f")
1402
- # #=> ["a", "b", "c", "d", "e", "f"]
1403
- # [1, 2, 3].push(4).push(5)
1404
- # #=> [1, 2, 3, 4, 5]
2649
+ # Appends each argument in `objects` to `self`; returns `self`:
2650
+ # a = [:foo, 'bar', 2]
2651
+ # a.push(:baz, :bat) # => [:foo, "bar", 2, :baz, :bat]
1405
2652
  #
1406
- def push: (*Elem obj) -> self
1407
-
1408
- # Searches through the array whose elements are also arrays.
2653
+ # Appends each argument as one element, even if it is another Array:
2654
+ # a = [:foo, 'bar', 2]
2655
+ # a1 = a.push([:baz, :bat], [:bam, :bad])
2656
+ # a1 # => [:foo, "bar", 2, [:baz, :bat], [:bam, :bad]]
2657
+ #
2658
+ # Array#append is an alias for Array#push.
1409
2659
  #
1410
- # Compares `obj` with the second element of each contained array using `obj.==`.
2660
+ # Related: #pop, #shift, #unshift.
1411
2661
  #
1412
- # Returns the first contained array that matches `obj`.
2662
+ def push: (*Elem obj) -> self
2663
+
2664
+ # <!--
2665
+ # rdoc-file=array.c
2666
+ # - array.rassoc(obj) -> found_array or nil
2667
+ # -->
2668
+ # Returns the first element in `self` that is an Array whose second element `==`
2669
+ # `obj`:
2670
+ # a = [{foo: 0}, [2, 4], [4, 5, 6], [4, 5]]
2671
+ # a.rassoc(4) # => [2, 4]
1413
2672
  #
1414
- # See also Array#assoc.
2673
+ # Returns `nil` if no such element is found.
1415
2674
  #
1416
- # a = [ [ 1, "one"], [2, "two"], [3, "three"], ["ii", "two"] ]
1417
- # a.rassoc("two") #=> [2, "two"]
1418
- # a.rassoc("four") #=> nil
2675
+ # Related: #assoc.
1419
2676
  #
1420
2677
  alias rassoc assoc
1421
2678
 
1422
- # Returns a new array containing the items in `self` for which the given block
1423
- # is not `true`. The ordering of non-rejected elements is maintained.
1424
- #
1425
- # See also Array#delete_if
1426
- #
1427
- # If no block is given, an Enumerator is returned instead.
2679
+ # <!--
2680
+ # rdoc-file=array.c
2681
+ # - array.reject {|element| ... } -> new_array
2682
+ # - array.reject -> new_enumerator
2683
+ # -->
2684
+ # Returns a new Array whose elements are all those from `self` for which the
2685
+ # block returns `false` or `nil`:
2686
+ # a = [:foo, 'bar', 2, 'bat']
2687
+ # a1 = a.reject {|element| element.to_s.start_with?('b') }
2688
+ # a1 # => [:foo, 2]
2689
+ #
2690
+ # Returns a new Enumerator if no block given:
2691
+ # a = [:foo, 'bar', 2]
2692
+ # a.reject # => #<Enumerator: [:foo, "bar", 2]:reject>
1428
2693
  #
1429
2694
  alias reject delete_if
1430
2695
 
1431
- # Deletes every element of `self` for which the block evaluates to `true`, if no
1432
- # changes were made returns `nil`.
2696
+ # <!--
2697
+ # rdoc-file=array.c
2698
+ # - array.reject! {|element| ... } -> self or nil
2699
+ # - array.reject! -> new_enumerator
2700
+ # -->
2701
+ # Removes each element for which the block returns a truthy value.
1433
2702
  #
1434
- # The array may not be changed instantly every time the block is called.
2703
+ # Returns `self` if any elements removed:
2704
+ # a = [:foo, 'bar', 2, 'bat']
2705
+ # a.reject! {|element| element.to_s.start_with?('b') } # => [:foo, 2]
1435
2706
  #
1436
- # See also Enumerable#reject and Array#delete_if.
2707
+ # Returns `nil` if no elements removed.
1437
2708
  #
1438
- # If no block is given, an Enumerator is returned instead.
2709
+ # Returns a new Enumerator if no block given:
2710
+ # a = [:foo, 'bar', 2]
2711
+ # a.reject! # => #<Enumerator: [:foo, "bar", 2]:reject!>
1439
2712
  #
1440
2713
  def reject!: () { (Elem item) -> boolish } -> self?
1441
2714
  | () -> ::Enumerator[Elem, self?]
1442
2715
 
1443
- # When invoked with a block, yields all repeated combinations of length `n` of
1444
- # elements from the array and then returns the array itself.
1445
- #
1446
- # The implementation makes no guarantees about the order in which the repeated
1447
- # combinations are yielded.
1448
- #
1449
- # If no block is given, an Enumerator is returned instead.
1450
- #
1451
- # Examples:
1452
- #
1453
- # a = [1, 2, 3]
1454
- # a.repeated_combination(1).to_a #=> [[1], [2], [3]]
1455
- # a.repeated_combination(2).to_a #=> [[1,1],[1,2],[1,3],[2,2],[2,3],[3,3]]
1456
- # a.repeated_combination(3).to_a #=> [[1,1,1],[1,1,2],[1,1,3],[1,2,2],[1,2,3],
1457
- # # [1,3,3],[2,2,2],[2,2,3],[2,3,3],[3,3,3]]
1458
- # a.repeated_combination(4).to_a #=> [[1,1,1,1],[1,1,1,2],[1,1,1,3],[1,1,2,2],[1,1,2,3],
1459
- # # [1,1,3,3],[1,2,2,2],[1,2,2,3],[1,2,3,3],[1,3,3,3],
1460
- # # [2,2,2,2],[2,2,2,3],[2,2,3,3],[2,3,3,3],[3,3,3,3]]
1461
- # a.repeated_combination(0).to_a #=> [[]] # one combination of length 0
2716
+ # <!--
2717
+ # rdoc-file=array.c
2718
+ # - array.repeated_combination(n) {|combination| ... } -> self
2719
+ # - array.repeated_combination(n) -> new_enumerator
2720
+ # -->
2721
+ # Calls the block with each repeated combination of length `n` of the elements
2722
+ # of `self`; each combination is an Array; returns `self`. The order of the
2723
+ # combinations is indeterminate.
2724
+ #
2725
+ # When a block and a positive Integer argument `n` are given, calls the block
2726
+ # with each `n`-tuple repeated combination of the elements of `self`. The number
2727
+ # of combinations is `(n+1)(n+2)/2`.
2728
+ #
2729
+ # `n` = 1:
2730
+ # a = [0, 1, 2]
2731
+ # a.repeated_combination(1) {|combination| p combination }
2732
+ #
2733
+ # Output:
2734
+ # [0]
2735
+ # [1]
2736
+ # [2]
2737
+ #
2738
+ # `n` = 2:
2739
+ # a.repeated_combination(2) {|combination| p combination }
2740
+ #
2741
+ # Output:
2742
+ # [0, 0]
2743
+ # [0, 1]
2744
+ # [0, 2]
2745
+ # [1, 1]
2746
+ # [1, 2]
2747
+ # [2, 2]
2748
+ #
2749
+ # If `n` is zero, calls the block once with an empty Array.
2750
+ #
2751
+ # If `n` is negative, does not call the block:
2752
+ # a.repeated_combination(-1) {|combination| fail 'Cannot happen' }
2753
+ #
2754
+ # Returns a new Enumerator if no block given:
2755
+ # a = [0, 1, 2]
2756
+ # a.repeated_combination(2) # => #<Enumerator: [0, 1, 2]:combination(2)>
2757
+ #
2758
+ # Using Enumerators, it's convenient to show the combinations and counts for
2759
+ # some values of `n`:
2760
+ # e = a.repeated_combination(0)
2761
+ # e.size # => 1
2762
+ # e.to_a # => [[]]
2763
+ # e = a.repeated_combination(1)
2764
+ # e.size # => 3
2765
+ # e.to_a # => [[0], [1], [2]]
2766
+ # e = a.repeated_combination(2)
2767
+ # e.size # => 6
2768
+ # e.to_a # => [[0, 0], [0, 1], [0, 2], [1, 1], [1, 2], [2, 2]]
1462
2769
  #
1463
2770
  def repeated_combination: (int n) { (::Array[Elem] c) -> void } -> self
1464
2771
  | (int n) -> ::Enumerator[::Array[Elem], self]
1465
2772
 
1466
- # When invoked with a block, yield all repeated permutations of length `n` of
1467
- # the elements of the array, then return the array itself.
1468
- #
1469
- # The implementation makes no guarantees about the order in which the repeated
1470
- # permutations are yielded.
1471
- #
1472
- # If no block is given, an Enumerator is returned instead.
1473
- #
1474
- # Examples:
1475
- #
1476
- # a = [1, 2]
1477
- # a.repeated_permutation(1).to_a #=> [[1], [2]]
1478
- # a.repeated_permutation(2).to_a #=> [[1,1],[1,2],[2,1],[2,2]]
1479
- # a.repeated_permutation(3).to_a #=> [[1,1,1],[1,1,2],[1,2,1],[1,2,2],
1480
- # # [2,1,1],[2,1,2],[2,2,1],[2,2,2]]
1481
- # a.repeated_permutation(0).to_a #=> [[]] # one permutation of length 0
2773
+ # <!--
2774
+ # rdoc-file=array.c
2775
+ # - array.repeated_permutation(n) {|permutation| ... } -> self
2776
+ # - array.repeated_permutation(n) -> new_enumerator
2777
+ # -->
2778
+ # Calls the block with each repeated permutation of length `n` of the elements
2779
+ # of `self`; each permutation is an Array; returns `self`. The order of the
2780
+ # permutations is indeterminate.
2781
+ #
2782
+ # When a block and a positive Integer argument `n` are given, calls the block
2783
+ # with each `n`-tuple repeated permutation of the elements of `self`. The number
2784
+ # of permutations is `self.size**n`.
2785
+ #
2786
+ # `n` = 1:
2787
+ # a = [0, 1, 2]
2788
+ # a.repeated_permutation(1) {|permutation| p permutation }
2789
+ #
2790
+ # Output:
2791
+ # [0]
2792
+ # [1]
2793
+ # [2]
2794
+ #
2795
+ # `n` = 2:
2796
+ # a.repeated_permutation(2) {|permutation| p permutation }
2797
+ #
2798
+ # Output:
2799
+ # [0, 0]
2800
+ # [0, 1]
2801
+ # [0, 2]
2802
+ # [1, 0]
2803
+ # [1, 1]
2804
+ # [1, 2]
2805
+ # [2, 0]
2806
+ # [2, 1]
2807
+ # [2, 2]
2808
+ #
2809
+ # If `n` is zero, calls the block once with an empty Array.
2810
+ #
2811
+ # If `n` is negative, does not call the block:
2812
+ # a.repeated_permutation(-1) {|permutation| fail 'Cannot happen' }
2813
+ #
2814
+ # Returns a new Enumerator if no block given:
2815
+ # a = [0, 1, 2]
2816
+ # a.repeated_permutation(2) # => #<Enumerator: [0, 1, 2]:permutation(2)>
2817
+ #
2818
+ # Using Enumerators, it's convenient to show the permutations and counts for
2819
+ # some values of `n`:
2820
+ # e = a.repeated_permutation(0)
2821
+ # e.size # => 1
2822
+ # e.to_a # => [[]]
2823
+ # e = a.repeated_permutation(1)
2824
+ # e.size # => 3
2825
+ # e.to_a # => [[0], [1], [2]]
2826
+ # e = a.repeated_permutation(2)
2827
+ # e.size # => 9
2828
+ # e.to_a # => [[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]]
1482
2829
  #
1483
2830
  def repeated_permutation: (int n) { (::Array[Elem] p) -> void } -> self
1484
2831
  | (int n) -> ::Enumerator[::Array[Elem], self]
1485
2832
 
1486
- # Replaces the contents of `self` with the contents of `other_ary`, truncating
1487
- # or expanding if necessary.
1488
- #
1489
- # a = [ "a", "b", "c", "d", "e" ]
1490
- # a.replace([ "x", "y", "z" ]) #=> ["x", "y", "z"]
1491
- # a #=> ["x", "y", "z"]
2833
+ # <!-- rdoc-file=array.c -->
2834
+ # Replaces the content of `self` with the content of `other_array`; returns
2835
+ # `self`:
2836
+ # a = [:foo, 'bar', 2]
2837
+ # a.replace(['foo', :bar, 3]) # => ["foo", :bar, 3]
1492
2838
  #
1493
2839
  def replace: (::Array[Elem]) -> self
1494
2840
 
1495
- # Returns a new array containing `self`'s elements in reverse order.
1496
- #
1497
- # [ "a", "b", "c" ].reverse #=> ["c", "b", "a"]
1498
- # [ 1 ].reverse #=> [1]
2841
+ # <!--
2842
+ # rdoc-file=array.c
2843
+ # - array.reverse -> new_array
2844
+ # -->
2845
+ # Returns a new Array with the elements of `self` in reverse order.
2846
+ # a = ['foo', 'bar', 'two']
2847
+ # a1 = a.reverse
2848
+ # a1 # => ["two", "bar", "foo"]
1499
2849
  #
1500
2850
  def reverse: () -> ::Array[Elem]
1501
2851
 
1502
- # Reverses `self` in place.
1503
- #
1504
- # a = [ "a", "b", "c" ]
1505
- # a.reverse! #=> ["c", "b", "a"]
1506
- # a #=> ["c", "b", "a"]
2852
+ # <!--
2853
+ # rdoc-file=array.c
2854
+ # - array.reverse! -> self
2855
+ # -->
2856
+ # Reverses `self` in place:
2857
+ # a = ['foo', 'bar', 'two']
2858
+ # a.reverse! # => ["two", "bar", "foo"]
1507
2859
  #
1508
2860
  def reverse!: () -> ::Array[Elem]
1509
2861
 
1510
- # Same as Array#each, but traverses `self` in reverse order.
2862
+ # <!--
2863
+ # rdoc-file=array.c
2864
+ # - array.reverse_each {|element| ... } -> self
2865
+ # - array.reverse_each -> Enumerator
2866
+ # -->
2867
+ # Iterates backwards over array elements.
1511
2868
  #
1512
- # a = [ "a", "b", "c" ]
1513
- # a.reverse_each {|x| print x, " " }
2869
+ # When a block given, passes, in reverse order, each element to the block;
2870
+ # returns `self`:
2871
+ # a = [:foo, 'bar', 2]
2872
+ # a.reverse_each {|element| puts "#{element.class} #{element}" }
2873
+ #
2874
+ # Output:
2875
+ # Integer 2
2876
+ # String bar
2877
+ # Symbol foo
2878
+ #
2879
+ # Allows the array to be modified during iteration:
2880
+ # a = [:foo, 'bar', 2]
2881
+ # a.reverse_each {|element| puts element; a.clear if element.to_s.start_with?('b') }
1514
2882
  #
1515
- # produces:
2883
+ # Output:
2884
+ # 2
2885
+ # bar
1516
2886
  #
1517
- # c b a
2887
+ # When no block given, returns a new Enumerator:
2888
+ # a = [:foo, 'bar', 2]
2889
+ # e = a.reverse_each
2890
+ # e # => #<Enumerator: [:foo, "bar", 2]:reverse_each>
2891
+ # a1 = e.each {|element| puts "#{element.class} #{element}" }
2892
+ #
2893
+ # Output:
2894
+ # Integer 2
2895
+ # String bar
2896
+ # Symbol foo
2897
+ #
2898
+ # Related: #each, #each_index.
1518
2899
  #
1519
2900
  def reverse_each: () { (Elem item) -> void } -> self
1520
2901
  | () -> ::Enumerator[Elem, self]
1521
2902
 
1522
- # Returns the *index* of the last object in `self` `==` to `obj`.
2903
+ # <!--
2904
+ # rdoc-file=array.c
2905
+ # - array.rindex(object) -> integer or nil
2906
+ # - array.rindex {|element| ... } -> integer or nil
2907
+ # - array.rindex -> new_enumerator
2908
+ # -->
2909
+ # Returns the index of the last element for which `object == element`.
2910
+ #
2911
+ # When argument `object` is given but no block, returns the index of the last
2912
+ # such element found:
2913
+ # a = [:foo, 'bar', 2, 'bar']
2914
+ # a.rindex('bar') # => 3
2915
+ #
2916
+ # Returns `nil` if no such object found.
1523
2917
  #
1524
- # If a block is given instead of an argument, returns the *index* of the first
1525
- # object for which the block returns `true`, starting from the last object.
2918
+ # When a block is given but no argument, calls the block with each successive
2919
+ # element; returns the index of the last element for which the block returns a
2920
+ # truthy value:
2921
+ # a = [:foo, 'bar', 2, 'bar']
2922
+ # a.rindex {|element| element == 'bar' } # => 3
1526
2923
  #
1527
- # Returns `nil` if no match is found.
2924
+ # Returns `nil` if the block never returns a truthy value.
1528
2925
  #
1529
- # See also Array#index.
2926
+ # When neither an argument nor a block is given, returns a new Enumerator:
1530
2927
  #
1531
- # If neither block nor argument is given, an Enumerator is returned instead.
2928
+ # a = [:foo, 'bar', 2, 'bar']
2929
+ # e = a.rindex
2930
+ # e # => #<Enumerator: [:foo, "bar", 2, "bar"]:rindex>
2931
+ # e.each {|element| element == 'bar' } # => 3
1532
2932
  #
1533
- # a = [ "a", "b", "b", "b", "c" ]
1534
- # a.rindex("b") #=> 3
1535
- # a.rindex("z") #=> nil
1536
- # a.rindex {|x| x == "b"} #=> 3
2933
+ # Related: #index.
1537
2934
  #
1538
2935
  def rindex: (untyped obj) -> ::Integer?
1539
2936
  | () { (Elem item) -> boolish } -> ::Integer?
1540
2937
  | () -> ::Enumerator[Elem, ::Integer?]
1541
2938
 
1542
- # Returns a new array by rotating `self` so that the element at `count` is the
1543
- # first element of the new array.
1544
- #
1545
- # If `count` is negative then it rotates in the opposite direction, starting
1546
- # from the end of `self` where `-1` is the last element.
1547
- #
1548
- # a = [ "a", "b", "c", "d" ]
1549
- # a.rotate #=> ["b", "c", "d", "a"]
1550
- # a #=> ["a", "b", "c", "d"]
1551
- # a.rotate(2) #=> ["c", "d", "a", "b"]
1552
- # a.rotate(-3) #=> ["b", "c", "d", "a"]
2939
+ # <!--
2940
+ # rdoc-file=array.c
2941
+ # - array.rotate -> new_array
2942
+ # - array.rotate(count) -> new_array
2943
+ # -->
2944
+ # Returns a new Array formed from `self` with elements rotated from one end to
2945
+ # the other.
2946
+ #
2947
+ # When no argument given, returns a new Array that is like `self`, except that
2948
+ # the first element has been rotated to the last position:
2949
+ # a = [:foo, 'bar', 2, 'bar']
2950
+ # a1 = a.rotate
2951
+ # a1 # => ["bar", 2, "bar", :foo]
2952
+ #
2953
+ # When given a non-negative Integer `count`, returns a new Array with `count`
2954
+ # elements rotated from the beginning to the end:
2955
+ # a = [:foo, 'bar', 2]
2956
+ # a1 = a.rotate(2)
2957
+ # a1 # => [2, :foo, "bar"]
2958
+ #
2959
+ # If `count` is large, uses `count % array.size` as the count:
2960
+ # a = [:foo, 'bar', 2]
2961
+ # a1 = a.rotate(20)
2962
+ # a1 # => [2, :foo, "bar"]
2963
+ #
2964
+ # If `count` is zero, returns a copy of `self`, unmodified:
2965
+ # a = [:foo, 'bar', 2]
2966
+ # a1 = a.rotate(0)
2967
+ # a1 # => [:foo, "bar", 2]
2968
+ #
2969
+ # When given a negative Integer `count`, rotates in the opposite direction, from
2970
+ # end to beginning:
2971
+ # a = [:foo, 'bar', 2]
2972
+ # a1 = a.rotate(-2)
2973
+ # a1 # => ["bar", 2, :foo]
2974
+ #
2975
+ # If `count` is small (far from zero), uses `count % array.size` as the count:
2976
+ # a = [:foo, 'bar', 2]
2977
+ # a1 = a.rotate(-5)
2978
+ # a1 # => ["bar", 2, :foo]
1553
2979
  #
1554
2980
  def rotate: (?int count) -> ::Array[Elem]
1555
2981
 
1556
- # Rotates `self` in place so that the element at `count` comes first, and
1557
- # returns `self`.
1558
- #
1559
- # If `count` is negative then it rotates in the opposite direction, starting
1560
- # from the end of the array where `-1` is the last element.
2982
+ # <!--
2983
+ # rdoc-file=array.c
2984
+ # - array.rotate! -> self
2985
+ # - array.rotate!(count) -> self
2986
+ # -->
2987
+ # Rotates `self` in place by moving elements from one end to the other; returns
2988
+ # `self`.
1561
2989
  #
1562
- # a = [ "a", "b", "c", "d" ]
1563
- # a.rotate! #=> ["b", "c", "d", "a"]
1564
- # a #=> ["b", "c", "d", "a"]
1565
- # a.rotate!(2) #=> ["d", "a", "b", "c"]
1566
- # a.rotate!(-3) #=> ["a", "b", "c", "d"]
2990
+ # When no argument given, rotates the first element to the last position:
2991
+ # a = [:foo, 'bar', 2, 'bar']
2992
+ # a.rotate! # => ["bar", 2, "bar", :foo]
2993
+ #
2994
+ # When given a non-negative Integer `count`, rotates `count` elements from the
2995
+ # beginning to the end:
2996
+ # a = [:foo, 'bar', 2]
2997
+ # a.rotate!(2)
2998
+ # a # => [2, :foo, "bar"]
2999
+ #
3000
+ # If `count` is large, uses `count % array.size` as the count:
3001
+ # a = [:foo, 'bar', 2]
3002
+ # a.rotate!(20)
3003
+ # a # => [2, :foo, "bar"]
3004
+ #
3005
+ # If `count` is zero, returns `self` unmodified:
3006
+ # a = [:foo, 'bar', 2]
3007
+ # a.rotate!(0)
3008
+ # a # => [:foo, "bar", 2]
3009
+ #
3010
+ # When given a negative Integer `count`, rotates in the opposite direction, from
3011
+ # end to beginning:
3012
+ # a = [:foo, 'bar', 2]
3013
+ # a.rotate!(-2)
3014
+ # a # => ["bar", 2, :foo]
3015
+ #
3016
+ # If `count` is small (far from zero), uses `count % array.size` as the count:
3017
+ # a = [:foo, 'bar', 2]
3018
+ # a.rotate!(-5)
3019
+ # a # => ["bar", 2, :foo]
1567
3020
  #
1568
3021
  def rotate!: (?int count) -> self
1569
3022
 
1570
- # Choose a random element or `n` random elements from the array.
3023
+ # <!--
3024
+ # rdoc-file=array.rb
3025
+ # - array.sample(random: Random) -> object
3026
+ # - array.sample(n, random: Random) -> new_ary
3027
+ # -->
3028
+ # Returns random elements from `self`.
1571
3029
  #
1572
- # The elements are chosen by using random and unique indices into the array in
1573
- # order to ensure that an element doesn't repeat itself unless the array already
1574
- # contained duplicate elements.
3030
+ # When no arguments are given, returns a random element from `self`:
3031
+ # a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
3032
+ # a.sample # => 3
3033
+ # a.sample # => 8
1575
3034
  #
1576
- # If the array is empty the first form returns `nil` and the second form returns
1577
- # an empty array.
3035
+ # If `self` is empty, returns `nil`.
1578
3036
  #
1579
- # a = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
1580
- # a.sample #=> 7
1581
- # a.sample(4) #=> [6, 4, 2, 5]
3037
+ # When argument `n` is given, returns a new Array containing `n` random elements
3038
+ # from `self`:
3039
+ # a.sample(3) # => [8, 9, 2]
3040
+ # a.sample(6) # => [9, 6, 10, 3, 1, 4]
1582
3041
  #
1583
- # The optional `rng` argument will be used as the random number generator.
3042
+ # Returns no more than `a.size` elements (because no new duplicates are
3043
+ # introduced):
3044
+ # a.sample(a.size * 2) # => [6, 4, 1, 8, 5, 9, 10, 2, 3, 7]
1584
3045
  #
3046
+ # But `self` may contain duplicates:
3047
+ # a = [1, 1, 1, 2, 2, 3]
3048
+ # a.sample(a.size * 2) # => [1, 1, 3, 2, 1, 2]
3049
+ #
3050
+ # The argument `n` must be a non-negative numeric value. The order of the result
3051
+ # array is unrelated to the order of `self`. Returns a new empty Array if `self`
3052
+ # is empty.
3053
+ #
3054
+ # The optional `random` argument will be used as the random number generator:
3055
+ # a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
1585
3056
  # a.sample(random: Random.new(1)) #=> 6
1586
3057
  # a.sample(4, random: Random.new(1)) #=> [6, 10, 9, 2]
1587
3058
  #
1588
3059
  def sample: (?random: _Rand rng) -> Elem?
1589
3060
  | (int n, ?random: _Rand rng) -> ::Array[Elem]
1590
3061
 
1591
- # Returns a new array containing all elements of `ary` for which the given
1592
- # `block` returns a true value.
1593
- #
1594
- # If no block is given, an Enumerator is returned instead.
1595
- #
1596
- # [1,2,3,4,5].select {|num| num.even? } #=> [2, 4]
1597
- #
1598
- # a = %w[ a b c d e f ]
1599
- # a.select {|v| v =~ /[aeiou]/ } #=> ["a", "e"]
1600
- #
1601
- # See also Enumerable#select.
3062
+ # <!--
3063
+ # rdoc-file=array.c
3064
+ # - array.select {|element| ... } -> new_array
3065
+ # - array.select -> new_enumerator
3066
+ # -->
3067
+ # Calls the block, if given, with each element of `self`; returns a new Array
3068
+ # containing those elements of `self` for which the block returns a truthy
3069
+ # value:
3070
+ # a = [:foo, 'bar', 2, :bam]
3071
+ # a1 = a.select {|element| element.to_s.start_with?('b') }
3072
+ # a1 # => ["bar", :bam]
3073
+ #
3074
+ # Returns a new Enumerator if no block given:
3075
+ # a = [:foo, 'bar', 2, :bam]
3076
+ # a.select # => #<Enumerator: [:foo, "bar", 2, :bam]:select>
1602
3077
  #
1603
3078
  # Array#filter is an alias for Array#select.
1604
3079
  #
1605
3080
  def select: () { (Elem item) -> boolish } -> ::Array[Elem]
1606
3081
  | () -> ::Enumerator[Elem, ::Array[Elem]]
1607
3082
 
1608
- # Invokes the given block passing in successive elements from `self`, deleting
1609
- # elements for which the block returns a `false` value.
1610
- #
1611
- # The array may not be changed instantly every time the block is called.
3083
+ # <!--
3084
+ # rdoc-file=array.c
3085
+ # - array.select! {|element| ... } -> self or nil
3086
+ # - array.select! -> new_enumerator
3087
+ # -->
3088
+ # Calls the block, if given with each element of `self`; removes from `self`
3089
+ # those elements for which the block returns `false` or `nil`.
1612
3090
  #
1613
- # If changes were made, it will return `self`, otherwise it returns `nil`.
3091
+ # Returns `self` if any elements were removed:
3092
+ # a = [:foo, 'bar', 2, :bam]
3093
+ # a.select! {|element| element.to_s.start_with?('b') } # => ["bar", :bam]
1614
3094
  #
1615
- # If no block is given, an Enumerator is returned instead.
3095
+ # Returns `nil` if no elements were removed.
1616
3096
  #
1617
- # See also Array#keep_if.
3097
+ # Returns a new Enumerator if no block given:
3098
+ # a = [:foo, 'bar', 2, :bam]
3099
+ # a.select! # => #<Enumerator: [:foo, "bar", 2, :bam]:select!>
1618
3100
  #
1619
3101
  # Array#filter! is an alias for Array#select!.
1620
3102
  #
1621
3103
  def select!: () { (Elem item) -> boolish } -> self?
1622
3104
  | () -> ::Enumerator[Elem, self?]
1623
3105
 
1624
- # Removes the first element of `self` and returns it (shifting all other
1625
- # elements down by one). Returns `nil` if the array is empty.
3106
+ # <!--
3107
+ # rdoc-file=array.c
3108
+ # - array.shift -> object or nil
3109
+ # - array.shift(n) -> new_array
3110
+ # -->
3111
+ # Removes and returns leading elements.
3112
+ #
3113
+ # When no argument is given, removes and returns the first element:
3114
+ # a = [:foo, 'bar', 2]
3115
+ # a.shift # => :foo
3116
+ # a # => ['bar', 2]
3117
+ #
3118
+ # Returns `nil` if `self` is empty.
3119
+ #
3120
+ # When positive Integer argument `n` is given, removes the first `n` elements;
3121
+ # returns those elements in a new Array:
3122
+ # a = [:foo, 'bar', 2]
3123
+ # a.shift(2) # => [:foo, 'bar']
3124
+ # a # => [2]
1626
3125
  #
1627
- # If a number `n` is given, returns an array of the first `n` elements (or less)
1628
- # just like `array.slice!(0, n)` does. With `ary` containing only the remainder
1629
- # elements, not including what was shifted to `new_ary`. See also Array#unshift
1630
- # for the opposite effect.
3126
+ # If `n` is as large as or larger than `self.length`, removes all elements;
3127
+ # returns those elements in a new Array:
3128
+ # a = [:foo, 'bar', 2]
3129
+ # a.shift(3) # => [:foo, 'bar', 2]
1631
3130
  #
1632
- # args = [ "-m", "-q", "filename" ]
1633
- # args.shift #=> "-m"
1634
- # args #=> ["-q", "filename"]
3131
+ # If `n` is zero, returns a new empty Array; `self` is unmodified.
1635
3132
  #
1636
- # args = [ "-m", "-q", "filename" ]
1637
- # args.shift(2) #=> ["-m", "-q"]
1638
- # args #=> ["filename"]
3133
+ # Related: #push, #pop, #unshift.
1639
3134
  #
1640
3135
  def shift: () -> Elem?
1641
3136
  | (int n) -> ::Array[Elem]
1642
3137
 
3138
+ # <!--
3139
+ # rdoc-file=array.rb
3140
+ # - array.shuffle(random: Random) -> new_ary
3141
+ # -->
1643
3142
  # Returns a new array with elements of `self` shuffled.
3143
+ # a = [1, 2, 3] #=> [1, 2, 3]
3144
+ # a.shuffle #=> [2, 3, 1]
3145
+ # a #=> [1, 2, 3]
1644
3146
  #
1645
- # a = [ 1, 2, 3 ] #=> [1, 2, 3]
1646
- # a.shuffle #=> [2, 3, 1]
1647
- # a #=> [1, 2, 3]
1648
- #
1649
- # The optional `rng` argument will be used as the random number generator.
1650
- #
3147
+ # The optional `random` argument will be used as the random number generator:
1651
3148
  # a.shuffle(random: Random.new(1)) #=> [1, 3, 2]
1652
3149
  #
1653
3150
  def shuffle: (?random: _Rand rng) -> ::Array[Elem]
1654
3151
 
1655
- # Shuffles elements in `self` in place.
1656
- #
1657
- # a = [ 1, 2, 3 ] #=> [1, 2, 3]
1658
- # a.shuffle! #=> [2, 3, 1]
1659
- # a #=> [2, 3, 1]
1660
- #
1661
- # The optional `rng` argument will be used as the random number generator.
3152
+ # <!--
3153
+ # rdoc-file=array.rb
3154
+ # - array.shuffle!(random: Random) -> array
3155
+ # -->
3156
+ # Shuffles the elements of `self` in place.
3157
+ # a = [1, 2, 3] #=> [1, 2, 3]
3158
+ # a.shuffle! #=> [2, 3, 1]
3159
+ # a #=> [2, 3, 1]
1662
3160
  #
3161
+ # The optional `random` argument will be used as the random number generator:
1663
3162
  # a.shuffle!(random: Random.new(1)) #=> [1, 3, 2]
1664
3163
  #
1665
3164
  def shuffle!: (?random: _Rand rng) -> self
1666
3165
 
3166
+ # <!-- rdoc-file=array.c -->
3167
+ # Returns the count of elements in `self`.
3168
+ #
1667
3169
  alias size length
1668
3170
 
1669
- # Element Reference --- Returns the element at `index`, or returns a subarray
1670
- # starting at the `start` index and continuing for `length` elements, or returns
1671
- # a subarray specified by `range` of indices.
1672
- #
1673
- # Negative indices count backward from the end of the array (-1 is the last
1674
- # element). For `start` and `range` cases the starting index is just before an
1675
- # element. Additionally, an empty array is returned when the starting index for
1676
- # an element range is at the end of the array.
1677
- #
1678
- # Returns `nil` if the index (or starting index) are out of range.
1679
- #
1680
- # a = [ "a", "b", "c", "d", "e" ]
1681
- # a[2] + a[0] + a[1] #=> "cab"
1682
- # a[6] #=> nil
1683
- # a[1, 2] #=> [ "b", "c" ]
1684
- # a[1..3] #=> [ "b", "c", "d" ]
1685
- # a[4..7] #=> [ "e" ]
1686
- # a[6..10] #=> nil
1687
- # a[-3, 3] #=> [ "c", "d", "e" ]
1688
- # # special cases
1689
- # a[5] #=> nil
1690
- # a[6, 1] #=> nil
1691
- # a[5, 1] #=> []
1692
- # a[5..10] #=> []
3171
+ # <!-- rdoc-file=array.c -->
3172
+ # Returns elements from `self`; does not modify `self`.
3173
+ #
3174
+ # When a single Integer argument `index` is given, returns the element at offset
3175
+ # `index`:
3176
+ # a = [:foo, 'bar', 2]
3177
+ # a[0] # => :foo
3178
+ # a[2] # => 2
3179
+ # a # => [:foo, "bar", 2]
3180
+ #
3181
+ # If `index` is negative, counts relative to the end of `self`:
3182
+ # a = [:foo, 'bar', 2]
3183
+ # a[-1] # => 2
3184
+ # a[-2] # => "bar"
3185
+ #
3186
+ # If `index` is out of range, returns `nil`.
3187
+ #
3188
+ # When two Integer arguments `start` and `length` are given, returns a new Array
3189
+ # of size `length` containing successive elements beginning at offset `start`:
3190
+ # a = [:foo, 'bar', 2]
3191
+ # a[0, 2] # => [:foo, "bar"]
3192
+ # a[1, 2] # => ["bar", 2]
3193
+ #
3194
+ # If `start + length` is greater than `self.length`, returns all elements from
3195
+ # offset `start` to the end:
3196
+ # a = [:foo, 'bar', 2]
3197
+ # a[0, 4] # => [:foo, "bar", 2]
3198
+ # a[1, 3] # => ["bar", 2]
3199
+ # a[2, 2] # => [2]
3200
+ #
3201
+ # If `start == self.size` and `length >= 0`, returns a new empty Array.
3202
+ #
3203
+ # If `length` is negative, returns `nil`.
3204
+ #
3205
+ # When a single Range argument `range` is given, treats `range.min` as `start`
3206
+ # above and `range.size` as `length` above:
3207
+ # a = [:foo, 'bar', 2]
3208
+ # a[0..1] # => [:foo, "bar"]
3209
+ # a[1..2] # => ["bar", 2]
3210
+ #
3211
+ # Special case: If `range.start == a.size`, returns a new empty Array.
3212
+ #
3213
+ # If `range.end` is negative, calculates the end index from the end:
3214
+ # a = [:foo, 'bar', 2]
3215
+ # a[0..-1] # => [:foo, "bar", 2]
3216
+ # a[0..-2] # => [:foo, "bar"]
3217
+ # a[0..-3] # => [:foo]
3218
+ #
3219
+ # If `range.start` is negative, calculates the start index from the end:
3220
+ # a = [:foo, 'bar', 2]
3221
+ # a[-1..2] # => [2]
3222
+ # a[-2..2] # => ["bar", 2]
3223
+ # a[-3..2] # => [:foo, "bar", 2]
3224
+ #
3225
+ # If `range.start` is larger than the array size, returns `nil`.
3226
+ # a = [:foo, 'bar', 2]
3227
+ # a[4..1] # => nil
3228
+ # a[4..0] # => nil
3229
+ # a[4..-1] # => nil
3230
+ #
3231
+ # When a single Enumerator::ArithmeticSequence argument `aseq` is given, returns
3232
+ # an Array of elements corresponding to the indexes produced by the sequence.
3233
+ # a = ['--', 'data1', '--', 'data2', '--', 'data3']
3234
+ # a[(1..).step(2)] # => ["data1", "data2", "data3"]
3235
+ #
3236
+ # Unlike slicing with range, if the start or the end of the arithmetic sequence
3237
+ # is larger than array size, throws RangeError.
3238
+ # a = ['--', 'data1', '--', 'data2', '--', 'data3']
3239
+ # a[(1..11).step(2)]
3240
+ # # RangeError (((1..11).step(2)) out of range)
3241
+ # a[(7..).step(2)]
3242
+ # # RangeError (((7..).step(2)) out of range)
3243
+ #
3244
+ # If given a single argument, and its type is not one of the listed, tries to
3245
+ # convert it to Integer, and raises if it is impossible:
3246
+ # a = [:foo, 'bar', 2]
3247
+ # # Raises TypeError (no implicit conversion of Symbol into Integer):
3248
+ # a[:foo]
3249
+ #
3250
+ # Array#slice is an alias for Array#[].
1693
3251
  #
1694
3252
  def slice: (int index) -> Elem?
1695
3253
  | (int start, int length) -> ::Array[Elem]?
1696
3254
  | (::Range[::Integer] range) -> ::Array[Elem]?
1697
3255
 
1698
- # Deletes the element(s) given by an `index` (optionally up to `length`
1699
- # elements) or by a `range`.
1700
- #
1701
- # Returns the deleted object (or objects), or `nil` if the `index` is out of
1702
- # range.
1703
- #
1704
- # a = [ "a", "b", "c" ]
1705
- # a.slice!(1) #=> "b"
1706
- # a #=> ["a", "c"]
1707
- # a.slice!(-1) #=> "c"
1708
- # a #=> ["a"]
1709
- # a.slice!(100) #=> nil
1710
- # a #=> ["a"]
3256
+ # <!--
3257
+ # rdoc-file=array.c
3258
+ # - array.slice!(n) -> object or nil
3259
+ # - array.slice!(start, length) -> new_array or nil
3260
+ # - array.slice!(range) -> new_array or nil
3261
+ # -->
3262
+ # Removes and returns elements from `self`.
3263
+ #
3264
+ # When the only argument is an Integer `n`, removes and returns the *nth*
3265
+ # element in `self`:
3266
+ # a = [:foo, 'bar', 2]
3267
+ # a.slice!(1) # => "bar"
3268
+ # a # => [:foo, 2]
3269
+ #
3270
+ # If `n` is negative, counts backwards from the end of `self`:
3271
+ # a = [:foo, 'bar', 2]
3272
+ # a.slice!(-1) # => 2
3273
+ # a # => [:foo, "bar"]
3274
+ #
3275
+ # If `n` is out of range, returns `nil`.
3276
+ #
3277
+ # When the only arguments are Integers `start` and `length`, removes `length`
3278
+ # elements from `self` beginning at offset `start`; returns the deleted objects
3279
+ # in a new Array:
3280
+ # a = [:foo, 'bar', 2]
3281
+ # a.slice!(0, 2) # => [:foo, "bar"]
3282
+ # a # => [2]
3283
+ #
3284
+ # If `start + length` exceeds the array size, removes and returns all elements
3285
+ # from offset `start` to the end:
3286
+ # a = [:foo, 'bar', 2]
3287
+ # a.slice!(1, 50) # => ["bar", 2]
3288
+ # a # => [:foo]
3289
+ #
3290
+ # If `start == a.size` and `length` is non-negative, returns a new empty Array.
3291
+ #
3292
+ # If `length` is negative, returns `nil`.
3293
+ #
3294
+ # When the only argument is a Range object `range`, treats `range.min` as
3295
+ # `start` above and `range.size` as `length` above:
3296
+ # a = [:foo, 'bar', 2]
3297
+ # a.slice!(1..2) # => ["bar", 2]
3298
+ # a # => [:foo]
3299
+ #
3300
+ # If `range.start == a.size`, returns a new empty Array.
3301
+ #
3302
+ # If `range.start` is larger than the array size, returns `nil`.
3303
+ #
3304
+ # If `range.end` is negative, counts backwards from the end of the array:
3305
+ # a = [:foo, 'bar', 2]
3306
+ # a.slice!(0..-2) # => [:foo, "bar"]
3307
+ # a # => [2]
3308
+ #
3309
+ # If `range.start` is negative, calculates the start index backwards from the
3310
+ # end of the array:
3311
+ # a = [:foo, 'bar', 2]
3312
+ # a.slice!(-2..2) # => ["bar", 2]
3313
+ # a # => [:foo]
1711
3314
  #
1712
3315
  def slice!: (int index) -> Elem?
1713
3316
  | (int start, int length) -> ::Array[Elem]?
1714
3317
  | (::Range[::Integer] range) -> ::Array[Elem]?
1715
3318
 
1716
- # Returns a new array created by sorting `self`.
1717
- #
1718
- # Comparisons for the sort will be done using the `<=>` operator or using an
1719
- # optional code block.
1720
- #
1721
- # The block must implement a comparison between `a` and `b` and return an
1722
- # integer less than 0 when `b` follows `a`, `0` when `a` and `b` are equivalent,
1723
- # or an integer greater than 0 when `a` follows `b`.
1724
- #
1725
- # The result is not guaranteed to be stable. When the comparison of two
1726
- # elements returns `0`, the order of the elements is unpredictable.
1727
- #
1728
- # ary = [ "d", "a", "e", "c", "b" ]
1729
- # ary.sort #=> ["a", "b", "c", "d", "e"]
1730
- # ary.sort {|a, b| b <=> a} #=> ["e", "d", "c", "b", "a"]
1731
- #
1732
- # To produce the reverse order, the following can also be used (and may be
1733
- # faster):
1734
- #
1735
- # ary.sort.reverse! #=> ["e", "d", "c", "b", "a"]
1736
- #
1737
- # See also Enumerable#sort_by.
3319
+ # <!--
3320
+ # rdoc-file=array.c
3321
+ # - array.sort -> new_array
3322
+ # - array.sort {|a, b| ... } -> new_array
3323
+ # -->
3324
+ # Returns a new Array whose elements are those from `self`, sorted.
3325
+ #
3326
+ # With no block, compares elements using operator `<=>` (see Comparable):
3327
+ # a = 'abcde'.split('').shuffle
3328
+ # a # => ["e", "b", "d", "a", "c"]
3329
+ # a1 = a.sort
3330
+ # a1 # => ["a", "b", "c", "d", "e"]
3331
+ #
3332
+ # With a block, calls the block with each element pair; for each element pair
3333
+ # `a` and `b`, the block should return an integer:
3334
+ # * Negative when `b` is to follow `a`.
3335
+ # * Zero when `a` and `b` are equivalent.
3336
+ # * Positive when `a` is to follow `b`.
3337
+ #
3338
+ #
3339
+ # Example:
3340
+ # a = 'abcde'.split('').shuffle
3341
+ # a # => ["e", "b", "d", "a", "c"]
3342
+ # a1 = a.sort {|a, b| a <=> b }
3343
+ # a1 # => ["a", "b", "c", "d", "e"]
3344
+ # a2 = a.sort {|a, b| b <=> a }
3345
+ # a2 # => ["e", "d", "c", "b", "a"]
3346
+ #
3347
+ # When the block returns zero, the order for `a` and `b` is indeterminate, and
3348
+ # may be unstable:
3349
+ # a = 'abcde'.split('').shuffle
3350
+ # a # => ["e", "b", "d", "a", "c"]
3351
+ # a1 = a.sort {|a, b| 0 }
3352
+ # a1 # => ["c", "e", "b", "d", "a"]
3353
+ #
3354
+ # Related: Enumerable#sort_by.
1738
3355
  #
1739
3356
  def sort: () -> ::Array[Elem]
1740
3357
  | () { (Elem a, Elem b) -> ::Integer } -> ::Array[Elem]
1741
3358
 
1742
- # Sorts `self` in place.
1743
- #
1744
- # Comparisons for the sort will be done using the `<=>` operator or using an
1745
- # optional code block.
1746
- #
1747
- # The block must implement a comparison between `a` and `b` and return an
1748
- # integer less than 0 when `b` follows `a`, `0` when `a` and `b` are equivalent,
1749
- # or an integer greater than 0 when `a` follows `b`.
1750
- #
1751
- # The result is not guaranteed to be stable. When the comparison of two
1752
- # elements returns `0`, the order of the elements is unpredictable.
1753
- #
1754
- # ary = [ "d", "a", "e", "c", "b" ]
1755
- # ary.sort! #=> ["a", "b", "c", "d", "e"]
1756
- # ary.sort! {|a, b| b <=> a} #=> ["e", "d", "c", "b", "a"]
1757
- #
1758
- # See also Enumerable#sort_by.
3359
+ # <!--
3360
+ # rdoc-file=array.c
3361
+ # - array.sort! -> self
3362
+ # - array.sort! {|a, b| ... } -> self
3363
+ # -->
3364
+ # Returns `self` with its elements sorted in place.
3365
+ #
3366
+ # With no block, compares elements using operator `<=>` (see Comparable):
3367
+ # a = 'abcde'.split('').shuffle
3368
+ # a # => ["e", "b", "d", "a", "c"]
3369
+ # a.sort!
3370
+ # a # => ["a", "b", "c", "d", "e"]
3371
+ #
3372
+ # With a block, calls the block with each element pair; for each element pair
3373
+ # `a` and `b`, the block should return an integer:
3374
+ # * Negative when `b` is to follow `a`.
3375
+ # * Zero when `a` and `b` are equivalent.
3376
+ # * Positive when `a` is to follow `b`.
3377
+ #
3378
+ #
3379
+ # Example:
3380
+ # a = 'abcde'.split('').shuffle
3381
+ # a # => ["e", "b", "d", "a", "c"]
3382
+ # a.sort! {|a, b| a <=> b }
3383
+ # a # => ["a", "b", "c", "d", "e"]
3384
+ # a.sort! {|a, b| b <=> a }
3385
+ # a # => ["e", "d", "c", "b", "a"]
3386
+ #
3387
+ # When the block returns zero, the order for `a` and `b` is indeterminate, and
3388
+ # may be unstable:
3389
+ # a = 'abcde'.split('').shuffle
3390
+ # a # => ["e", "b", "d", "a", "c"]
3391
+ # a.sort! {|a, b| 0 }
3392
+ # a # => ["d", "e", "c", "a", "b"]
1759
3393
  #
1760
3394
  def sort!: () -> self
1761
3395
  | () { (Elem a, Elem b) -> ::Integer } -> self
1762
3396
 
1763
- # Sorts `self` in place using a set of keys generated by mapping the values in
1764
- # `self` through the given block.
3397
+ # <!--
3398
+ # rdoc-file=array.c
3399
+ # - array.sort_by! {|element| ... } -> self
3400
+ # - array.sort_by! -> new_enumerator
3401
+ # -->
3402
+ # Sorts the elements of `self` in place, using an ordering determined by the
3403
+ # block; returns self.
3404
+ #
3405
+ # Calls the block with each successive element; sorts elements based on the
3406
+ # values returned from the block.
1765
3407
  #
1766
- # The result is not guaranteed to be stable. When two keys are equal, the order
1767
- # of the corresponding elements is unpredictable.
3408
+ # For duplicates returned by the block, the ordering is indeterminate, and may
3409
+ # be unstable.
1768
3410
  #
1769
- # If no block is given, an Enumerator is returned instead.
3411
+ # This example sorts strings based on their sizes:
3412
+ # a = ['aaaa', 'bbb', 'cc', 'd']
3413
+ # a.sort_by! {|element| element.size }
3414
+ # a # => ["d", "cc", "bbb", "aaaa"]
1770
3415
  #
1771
- # See also Enumerable#sort_by.
3416
+ # Returns a new Enumerator if no block given:
3417
+ #
3418
+ # a = ['aaaa', 'bbb', 'cc', 'd']
3419
+ # a.sort_by! # => #<Enumerator: ["aaaa", "bbb", "cc", "d"]:sort_by!>
1772
3420
  #
1773
3421
  def sort_by!: [U] () { (Elem obj) -> U } -> ::Array[Elem]
1774
3422
  | () -> ::Enumerator[Elem, ::Array[Elem]]
1775
3423
 
1776
- # Returns the sum of elements. For example, [e1, e2, e3].sum returns init + e1 +
1777
- # e2 + e3.
1778
- #
1779
- # If a block is given, the block is applied to each element before addition.
3424
+ # <!--
3425
+ # rdoc-file=array.c
3426
+ # - array.sum(init = 0) -> object
3427
+ # - array.sum(init = 0) {|element| ... } -> object
3428
+ # -->
3429
+ # When no block is given, returns the object equivalent to:
3430
+ # sum = init
3431
+ # array.each {|element| sum += element }
3432
+ # sum
1780
3433
  #
1781
- # If *ary* is empty, it returns *init*.
3434
+ # For example, `[e1, e2, e3].sum` returns `init + e1 + e2 + e3`.
1782
3435
  #
1783
- # [].sum #=> 0
1784
- # [].sum(0.0) #=> 0.0
1785
- # [1, 2, 3].sum #=> 6
1786
- # [3, 5.5].sum #=> 8.5
1787
- # [2.5, 3.0].sum(0.0) {|e| e * e } #=> 15.25
1788
- # [Object.new].sum #=> TypeError
1789
- #
1790
- # The (arithmetic) mean value of an array can be obtained as follows.
1791
- #
1792
- # mean = ary.sum(0.0) / ary.length
1793
- #
1794
- # This method can be used for non-numeric objects by explicit *init* argument.
1795
- #
1796
- # ["a", "b", "c"].sum("") #=> "abc"
1797
- # [[1], [[2]], [3]].sum([]) #=> [1, [2], 3]
1798
- #
1799
- # However, Array#join and Array#flatten is faster than Array#sum for array of
1800
- # strings and array of arrays.
1801
- #
1802
- # ["a", "b", "c"].join #=> "abc"
1803
- # [[1], [[2]], [3]].flatten(1) #=> [1, [2], 3]
1804
- #
1805
- # Array#sum method may not respect method redefinition of "+" methods such as
1806
- # Integer#+.
3436
+ # Examples:
3437
+ # a = [0, 1, 2, 3]
3438
+ # a.sum # => 6
3439
+ # a.sum(100) # => 106
3440
+ #
3441
+ # The elements need not be numeric, but must be `+`-compatible with each other
3442
+ # and with `init`:
3443
+ # a = ['abc', 'def', 'ghi']
3444
+ # a.sum('jkl') # => "jklabcdefghi"
3445
+ #
3446
+ # When a block is given, it is called with each element and the block's return
3447
+ # value (instead of the element itself) is used as the addend:
3448
+ # a = ['zero', 1, :two]
3449
+ # s = a.sum('Coerced and concatenated: ') {|element| element.to_s }
3450
+ # s # => "Coerced and concatenated: zero1two"
3451
+ #
3452
+ # Notes:
3453
+ # * Array#join and Array#flatten may be faster than Array#sum for an Array of
3454
+ # Strings or an Array of Arrays.
3455
+ # * Array#sum method may not respect method redefinition of "+" methods such
3456
+ # as Integer#+.
1807
3457
  #
1808
3458
  def sum: (?untyped init) -> untyped
1809
3459
  | (?untyped init) { (Elem e) -> untyped } -> untyped
1810
3460
 
1811
- # Returns first `n` elements from the array.
1812
- #
1813
- # If a negative number is given, raises an ArgumentError.
1814
- #
1815
- # See also Array#drop
3461
+ # <!--
3462
+ # rdoc-file=array.c
3463
+ # - array.take(n) -> new_array
3464
+ # -->
3465
+ # Returns a new Array containing the first `n` element of `self`, where `n` is a
3466
+ # non-negative Integer; does not modify `self`.
1816
3467
  #
1817
- # a = [1, 2, 3, 4, 5, 0]
1818
- # a.take(3) #=> [1, 2, 3]
3468
+ # Examples:
3469
+ # a = [0, 1, 2, 3, 4, 5]
3470
+ # a.take(1) # => [0]
3471
+ # a.take(2) # => [0, 1]
3472
+ # a.take(50) # => [0, 1, 2, 3, 4, 5]
3473
+ # a # => [0, 1, 2, 3, 4, 5]
1819
3474
  #
1820
3475
  def take: (int n) -> ::Array[Elem]
1821
3476
 
1822
- # Passes elements to the block until the block returns `nil` or `false`, then
1823
- # stops iterating and returns an array of all prior elements.
1824
- #
1825
- # If no block is given, an Enumerator is returned instead.
1826
- #
1827
- # See also Array#drop_while
1828
- #
1829
- # a = [1, 2, 3, 4, 5, 0]
1830
- # a.take_while {|i| i < 3} #=> [1, 2]
3477
+ # <!--
3478
+ # rdoc-file=array.c
3479
+ # - array.take_while {|element| ... } -> new_array
3480
+ # - array.take_while -> new_enumerator
3481
+ # -->
3482
+ # Returns a new Array containing zero or more leading elements of `self`; does
3483
+ # not modify `self`.
3484
+ #
3485
+ # With a block given, calls the block with each successive element of `self`;
3486
+ # stops if the block returns `false` or `nil`; returns a new Array containing
3487
+ # those elements for which the block returned a truthy value:
3488
+ # a = [0, 1, 2, 3, 4, 5]
3489
+ # a.take_while {|element| element < 3 } # => [0, 1, 2]
3490
+ # a.take_while {|element| true } # => [0, 1, 2, 3, 4, 5]
3491
+ # a # => [0, 1, 2, 3, 4, 5]
3492
+ #
3493
+ # With no block given, returns a new Enumerator:
3494
+ # [0, 1].take_while # => #<Enumerator: [0, 1]:take_while>
1831
3495
  #
1832
3496
  def take_while: () { (Elem obj) -> boolish } -> ::Array[Elem]
1833
3497
  | () -> ::Enumerator[Elem, ::Array[Elem]]
1834
3498
 
1835
- # Returns `self`.
1836
- #
1837
- # If called on a subclass of Array, converts the receiver to an Array object.
3499
+ # <!--
3500
+ # rdoc-file=array.c
3501
+ # - to_a -> self or new_array
3502
+ # -->
3503
+ # When `self` is an instance of Array, returns `self`:
3504
+ # a = [:foo, 'bar', 2]
3505
+ # a.to_a # => [:foo, "bar", 2]
3506
+ #
3507
+ # Otherwise, returns a new Array containing the elements of `self`:
3508
+ # class MyArray < Array; end
3509
+ # a = MyArray.new(['foo', 'bar', 'two'])
3510
+ # a.instance_of?(Array) # => false
3511
+ # a.kind_of?(Array) # => true
3512
+ # a1 = a.to_a
3513
+ # a1 # => ["foo", "bar", "two"]
3514
+ # a1.class # => Array # Not MyArray
1838
3515
  #
1839
3516
  def to_a: () -> ::Array[Elem]
1840
3517
 
3518
+ # <!--
3519
+ # rdoc-file=array.c
3520
+ # - array.to_ary -> self
3521
+ # -->
1841
3522
  # Returns `self`.
1842
3523
  #
1843
3524
  def to_ary: () -> self
1844
3525
 
1845
- # Returns the result of interpreting *ary* as an array of `[key, value]` pairs.
1846
- #
1847
- # [[:foo, :bar], [1, 2]].to_h
1848
- # # => {:foo => :bar, 1 => 2}
1849
- #
1850
- # If a block is given, the results of the block on each element of the array
1851
- # will be used as pairs.
1852
- #
1853
- # ["foo", "bar"].to_h {|s| [s.ord, s]}
1854
- # # => {102=>"foo", 98=>"bar"}
3526
+ # <!--
3527
+ # rdoc-file=array.c
3528
+ # - array.to_h -> new_hash
3529
+ # - array.to_h {|item| ... } -> new_hash
3530
+ # -->
3531
+ # Returns a new Hash formed from `self`.
3532
+ #
3533
+ # When a block is given, calls the block with each array element; the block must
3534
+ # return a 2-element Array whose two elements form a key-value pair in the
3535
+ # returned Hash:
3536
+ # a = ['foo', :bar, 1, [2, 3], {baz: 4}]
3537
+ # h = a.to_h {|item| [item, item] }
3538
+ # h # => {"foo"=>"foo", :bar=>:bar, 1=>1, [2, 3]=>[2, 3], {:baz=>4}=>{:baz=>4}}
3539
+ #
3540
+ # When no block is given, `self` must be an Array of 2-element sub-arrays, each
3541
+ # sub-array is formed into a key-value pair in the new Hash:
3542
+ # [].to_h # => {}
3543
+ # a = [['foo', 'zero'], ['bar', 'one'], ['baz', 'two']]
3544
+ # h = a.to_h
3545
+ # h # => {"foo"=>"zero", "bar"=>"one", "baz"=>"two"}
1855
3546
  #
1856
3547
  def to_h: () -> Hash[untyped, untyped]
1857
- | [T, S] () { (Elem) -> [T, S] } -> Hash[T, S]
3548
+ | [T, S] () { (Elem) -> [ T, S ] } -> Hash[T, S]
1858
3549
 
1859
- alias to_s inspect
1860
-
1861
- # Assumes that `self` is an array of arrays and transposes the rows and columns.
3550
+ # <!-- rdoc-file=array.c -->
3551
+ # Returns the new String formed by calling method `#inspect` on each array
3552
+ # element:
3553
+ # a = [:foo, 'bar', 2]
3554
+ # a.inspect # => "[:foo, \"bar\", 2]"
1862
3555
  #
1863
- # a = [[1,2], [3,4], [5,6]]
1864
- # a.transpose #=> [[1, 3, 5], [2, 4, 6]]
3556
+ # Array#to_s is an alias for Array#inspect.
1865
3557
  #
1866
- # If the length of the subarrays don't match, an IndexError is raised.
3558
+ alias to_s inspect
3559
+
3560
+ # <!--
3561
+ # rdoc-file=array.c
3562
+ # - array.transpose -> new_array
3563
+ # -->
3564
+ # Transposes the rows and columns in an Array of Arrays; the nested Arrays must
3565
+ # all be the same size:
3566
+ # a = [[:a0, :a1], [:b0, :b1], [:c0, :c1]]
3567
+ # a.transpose # => [[:a0, :b0, :c0], [:a1, :b1, :c1]]
1867
3568
  #
1868
3569
  def transpose: () -> ::Array[::Array[untyped]]
1869
3570
 
1870
- # Set Union --- Returns a new array by joining `other_ary`s with `self`,
1871
- # excluding any duplicates and preserving the order from the given arrays.
1872
- #
1873
- # It compares elements using their #hash and #eql? methods for efficiency.
3571
+ # <!--
3572
+ # rdoc-file=array.c
3573
+ # - array.union(*other_arrays) -> new_array
3574
+ # -->
3575
+ # Returns a new Array that is the union of `self` and all given Arrays
3576
+ # `other_arrays`; duplicates are removed; order is preserved; items are
3577
+ # compared using `eql?`:
3578
+ # [0, 1, 2, 3].union([4, 5], [6, 7]) # => [0, 1, 2, 3, 4, 5, 6, 7]
3579
+ # [0, 1, 1].union([2, 1], [3, 1]) # => [0, 1, 2, 3]
3580
+ # [0, 1, 2, 3].union([3, 2], [1, 0]) # => [0, 1, 2, 3]
1874
3581
  #
1875
- # [ "a", "b", "c" ].union( [ "c", "d", "a" ] ) #=> [ "a", "b", "c", "d" ]
1876
- # [ "a" ].union( ["e", "b"], ["a", "c", "b"] ) #=> [ "a", "e", "b", "c" ]
1877
- # [ "a" ].union #=> [ "a" ]
3582
+ # Returns a copy of `self` if no arguments given.
1878
3583
  #
1879
- # See also Array#|.
3584
+ # Related: Array#|.
1880
3585
  #
1881
3586
  def union: [T] (*::Array[T] other_arys) -> ::Array[T | Elem]
1882
3587
 
1883
- # Returns a new array by removing duplicate values in `self`.
1884
- #
1885
- # If a block is given, it will use the return value of the block for comparison.
1886
- #
1887
- # It compares values using their #hash and #eql? methods for efficiency.
1888
- #
1889
- # `self` is traversed in order, and the first occurrence is kept.
1890
- #
1891
- # a = [ "a", "a", "b", "b", "c" ]
1892
- # a.uniq # => ["a", "b", "c"]
1893
- #
1894
- # b = [["student","sam"], ["student","george"], ["teacher","matz"]]
1895
- # b.uniq {|s| s.first} # => [["student", "sam"], ["teacher", "matz"]]
3588
+ # <!--
3589
+ # rdoc-file=array.c
3590
+ # - array.uniq -> new_array
3591
+ # - array.uniq {|element| ... } -> new_array
3592
+ # -->
3593
+ # Returns a new Array containing those elements from `self` that are not
3594
+ # duplicates, the first occurrence always being retained.
3595
+ #
3596
+ # With no block given, identifies and omits duplicates using method `eql?` to
3597
+ # compare.
3598
+ # a = [0, 0, 1, 1, 2, 2]
3599
+ # a.uniq # => [0, 1, 2]
3600
+ #
3601
+ # With a block given, calls the block for each element; identifies (using method
3602
+ # `eql?`) and omits duplicate values, that is, those elements for which the
3603
+ # block returns the same value:
3604
+ # a = ['a', 'aa', 'aaa', 'b', 'bb', 'bbb']
3605
+ # a.uniq {|element| element.size } # => ["a", "aa", "aaa"]
1896
3606
  #
1897
3607
  def uniq: () -> ::Array[Elem]
1898
3608
  | () { (Elem item) -> untyped } -> ::Array[Elem]
1899
3609
 
1900
- # Removes duplicate elements from `self`.
3610
+ # <!--
3611
+ # rdoc-file=array.c
3612
+ # - array.uniq! -> self or nil
3613
+ # - array.uniq! {|element| ... } -> self or nil
3614
+ # -->
3615
+ # Removes duplicate elements from `self`, the first occurrence always being
3616
+ # retained; returns `self` if any elements removed, `nil` otherwise.
1901
3617
  #
1902
- # If a block is given, it will use the return value of the block for comparison.
3618
+ # With no block given, identifies and removes elements using method `eql?` to
3619
+ # compare.
1903
3620
  #
1904
- # It compares values using their #hash and #eql? methods for efficiency.
3621
+ # Returns `self` if any elements removed:
3622
+ # a = [0, 0, 1, 1, 2, 2]
3623
+ # a.uniq! # => [0, 1, 2]
1905
3624
  #
1906
- # `self` is traversed in order, and the first occurrence is kept.
3625
+ # Returns `nil` if no elements removed.
1907
3626
  #
1908
- # Returns `nil` if no changes are made (that is, no duplicates are found).
3627
+ # With a block given, calls the block for each element; identifies (using method
3628
+ # `eql?`) and removes elements for which the block returns duplicate values.
1909
3629
  #
1910
- # a = [ "a", "a", "b", "b", "c" ]
1911
- # a.uniq! # => ["a", "b", "c"]
3630
+ # Returns `self` if any elements removed:
3631
+ # a = ['a', 'aa', 'aaa', 'b', 'bb', 'bbb']
3632
+ # a.uniq! {|element| element.size } # => ['a', 'aa', 'aaa']
1912
3633
  #
1913
- # b = [ "a", "b", "c" ]
1914
- # b.uniq! # => nil
1915
- #
1916
- # c = [["student","sam"], ["student","george"], ["teacher","matz"]]
1917
- # c.uniq! {|s| s.first} # => [["student", "sam"], ["teacher", "matz"]]
3634
+ # Returns `nil` if no elements removed.
1918
3635
  #
1919
3636
  def uniq!: () -> self?
1920
3637
  | () { (Elem) -> untyped } -> self?
1921
3638
 
1922
- # Prepends objects to the front of `self`, moving other elements upwards. See
1923
- # also Array#shift for the opposite effect.
3639
+ # <!--
3640
+ # rdoc-file=array.c
3641
+ # - array.unshift(*objects) -> self
3642
+ # -->
3643
+ # Prepends the given `objects` to `self`:
3644
+ # a = [:foo, 'bar', 2]
3645
+ # a.unshift(:bam, :bat) # => [:bam, :bat, :foo, "bar", 2]
3646
+ #
3647
+ # Array#prepend is an alias for Array#unshift.
1924
3648
  #
1925
- # a = [ "b", "c", "d" ]
1926
- # a.unshift("a") #=> ["a", "b", "c", "d"]
1927
- # a.unshift(1, 2) #=> [ 1, 2, "a", "b", "c", "d"]
3649
+ # Related: #push, #pop, #shift.
1928
3650
  #
1929
3651
  def unshift: (*Elem obj) -> self
1930
3652
 
1931
- # Returns an array containing the elements in `self` corresponding to the given
1932
- # `selector`(s).
3653
+ # <!--
3654
+ # rdoc-file=array.c
3655
+ # - array.values_at(*indexes) -> new_array
3656
+ # -->
3657
+ # Returns a new Array whose elements are the elements of `self` at the given
3658
+ # Integer or Range `indexes`.
1933
3659
  #
1934
- # The selectors may be either integer indices or ranges.
3660
+ # For each positive `index`, returns the element at offset `index`:
3661
+ # a = [:foo, 'bar', 2]
3662
+ # a.values_at(0, 2) # => [:foo, 2]
3663
+ # a.values_at(0..1) # => [:foo, "bar"]
1935
3664
  #
1936
- # See also Array#select.
3665
+ # The given `indexes` may be in any order, and may repeat:
3666
+ # a = [:foo, 'bar', 2]
3667
+ # a.values_at(2, 0, 1, 0, 2) # => [2, :foo, "bar", :foo, 2]
3668
+ # a.values_at(1, 0..2) # => ["bar", :foo, "bar", 2]
1937
3669
  #
1938
- # a = %w{ a b c d e f }
1939
- # a.values_at(1, 3, 5) # => ["b", "d", "f"]
1940
- # a.values_at(1, 3, 5, 7) # => ["b", "d", "f", nil]
1941
- # a.values_at(-1, -2, -2, -7) # => ["f", "e", "e", nil]
1942
- # a.values_at(4..6, 3...6) # => ["e", "f", nil, "d", "e", "f"]
3670
+ # Assigns `nil` for an `index` that is too large:
3671
+ # a = [:foo, 'bar', 2]
3672
+ # a.values_at(0, 3, 1, 3) # => [:foo, nil, "bar", nil]
1943
3673
  #
1944
- def values_at: (*int | ::Range[::Integer] selector) -> ::Array[Elem?]
1945
-
1946
- # Converts any arguments to arrays, then merges elements of `self` with
1947
- # corresponding elements from each argument.
3674
+ # Returns a new empty Array if no arguments given.
1948
3675
  #
1949
- # This generates a sequence of `ary.size` *n*-element arrays, where *n* is one
1950
- # more than the count of arguments.
3676
+ # For each negative `index`, counts backward from the end of the array:
3677
+ # a = [:foo, 'bar', 2]
3678
+ # a.values_at(-1, -3) # => [2, :foo]
1951
3679
  #
1952
- # If the size of any argument is less than the size of the initial array, `nil`
1953
- # values are supplied.
3680
+ # Assigns `nil` for an `index` that is too small:
3681
+ # a = [:foo, 'bar', 2]
3682
+ # a.values_at(0, -5, 1, -6, 2) # => [:foo, nil, "bar", nil, 2]
1954
3683
  #
1955
- # If a block is given, it is invoked for each output `array`, otherwise an array
1956
- # of arrays is returned.
3684
+ # The given `indexes` may have a mixture of signs:
3685
+ # a = [:foo, 'bar', 2]
3686
+ # a.values_at(0, -2, 1, -1) # => [:foo, "bar", "bar", 2]
1957
3687
  #
1958
- # a = [ 4, 5, 6 ]
1959
- # b = [ 7, 8, 9 ]
1960
- # [1, 2, 3].zip(a, b) #=> [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
1961
- # [1, 2].zip(a, b) #=> [[1, 4, 7], [2, 5, 8]]
1962
- # a.zip([1, 2], [8]) #=> [[4, 1, 8], [5, 2, nil], [6, nil, nil]]
3688
+ def values_at: (*int | ::Range[::Integer] selector) -> ::Array[Elem?]
3689
+
3690
+ # <!--
3691
+ # rdoc-file=array.c
3692
+ # - array.zip(*other_arrays) -> new_array
3693
+ # - array.zip(*other_arrays) {|other_array| ... } -> nil
3694
+ # -->
3695
+ # When no block given, returns a new Array `new_array` of size `self.size` whose
3696
+ # elements are Arrays.
3697
+ #
3698
+ # Each nested array `new_array[n]` is of size `other_arrays.size+1`, and
3699
+ # contains:
3700
+ # * The *nth* element of `self`.
3701
+ # * The *nth* element of each of the `other_arrays`.
3702
+ #
3703
+ #
3704
+ # If all `other_arrays` and `self` are the same size:
3705
+ # a = [:a0, :a1, :a2, :a3]
3706
+ # b = [:b0, :b1, :b2, :b3]
3707
+ # c = [:c0, :c1, :c2, :c3]
3708
+ # d = a.zip(b, c)
3709
+ # d # => [[:a0, :b0, :c0], [:a1, :b1, :c1], [:a2, :b2, :c2], [:a3, :b3, :c3]]
3710
+ #
3711
+ # If any array in `other_arrays` is smaller than `self`, fills to `self.size`
3712
+ # with `nil`:
3713
+ # a = [:a0, :a1, :a2, :a3]
3714
+ # b = [:b0, :b1, :b2]
3715
+ # c = [:c0, :c1]
3716
+ # d = a.zip(b, c)
3717
+ # d # => [[:a0, :b0, :c0], [:a1, :b1, :c1], [:a2, :b2, nil], [:a3, nil, nil]]
3718
+ #
3719
+ # If any array in `other_arrays` is larger than `self`, its trailing elements
3720
+ # are ignored:
3721
+ # a = [:a0, :a1, :a2, :a3]
3722
+ # b = [:b0, :b1, :b2, :b3, :b4]
3723
+ # c = [:c0, :c1, :c2, :c3, :c4, :c5]
3724
+ # d = a.zip(b, c)
3725
+ # d # => [[:a0, :b0, :c0], [:a1, :b1, :c1], [:a2, :b2, :c2], [:a3, :b3, :c3]]
3726
+ #
3727
+ # When a block is given, calls the block with each of the sub-arrays (formed as
3728
+ # above); returns nil
3729
+ # a = [:a0, :a1, :a2, :a3]
3730
+ # b = [:b0, :b1, :b2, :b3]
3731
+ # c = [:c0, :c1, :c2, :c3]
3732
+ # a.zip(b, c) {|sub_array| p sub_array} # => nil
3733
+ #
3734
+ # Output:
3735
+ # [:a0, :b0, :c0]
3736
+ # [:a1, :b1, :c1]
3737
+ # [:a2, :b2, :c2]
3738
+ # [:a3, :b3, :c3]
1963
3739
  #
1964
3740
  def zip: [U] (::Array[U] arg) -> ::Array[[ Elem, U? ]]
1965
3741
  | (::Array[untyped] arg, *::Array[untyped] args) -> ::Array[::Array[untyped]]
1966
- | [U] (::Array[U] arg) { ([Elem, U?]) -> void } -> void
3742
+ | [U] (::Array[U] arg) { ([ Elem, U? ]) -> void } -> void
1967
3743
  | (::Array[untyped] arg, *::Array[untyped] args) { (::Array[untyped]) -> void } -> void
1968
3744
 
1969
- # Set Union --- Returns a new array by joining `ary` with `other_ary`, excluding
1970
- # any duplicates and preserving the order from the given arrays.
1971
- #
1972
- # It compares elements using their #hash and #eql? methods for efficiency.
3745
+ # <!--
3746
+ # rdoc-file=array.c
3747
+ # - array | other_array -> new_array
3748
+ # -->
3749
+ # Returns the union of `array` and Array `other_array`; duplicates are removed;
3750
+ # order is preserved; items are compared using `eql?`:
3751
+ # [0, 1] | [2, 3] # => [0, 1, 2, 3]
3752
+ # [0, 1, 1] | [2, 2, 3] # => [0, 1, 2, 3]
3753
+ # [0, 1, 2] | [3, 2, 1, 0] # => [0, 1, 2, 3]
1973
3754
  #
1974
- # [ "a", "b", "c" ] | [ "c", "d", "a" ] #=> [ "a", "b", "c", "d" ]
1975
- # [ "c", "d", "a" ] | [ "a", "b", "c" ] #=> [ "c", "d", "a", "b" ]
1976
- #
1977
- # See also Array#union.
3755
+ # Related: Array#union.
1978
3756
  #
1979
3757
  def |: [T] (::Array[T] other_ary) -> ::Array[Elem | T]
1980
3758
 
1981
3759
  private
1982
3760
 
1983
- # Replaces the contents of `self` with the contents of `other_ary`, truncating
1984
- # or expanding if necessary.
1985
- #
1986
- # a = [ "a", "b", "c", "d", "e" ]
1987
- # a.replace([ "x", "y", "z" ]) #=> ["x", "y", "z"]
1988
- # a #=> ["x", "y", "z"]
3761
+ # <!--
3762
+ # rdoc-file=array.c
3763
+ # - array.replace(other_array) -> self
3764
+ # -->
3765
+ # Replaces the content of `self` with the content of `other_array`; returns
3766
+ # `self`:
3767
+ # a = [:foo, 'bar', 2]
3768
+ # a.replace(['foo', :bar, 3]) # => ["foo", :bar, 3]
1989
3769
  #
1990
3770
  def initialize_copy: (self other_ary) -> void
1991
3771
  end