-
Notifications
You must be signed in to change notification settings - Fork 238
Expand file tree
/
Copy patharray.rbs
More file actions
4142 lines (4024 loc) · 141 KB
/
array.rbs
File metadata and controls
4142 lines (4024 loc) · 141 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# <!-- rdoc-file=array.c -->
# An Array object is an ordered, integer-indexed collection of objects, called
# *elements*; the object represents an [array data
# structure](https://en.wikipedia.org/wiki/Array_(data_structure)).
#
# An element may be any object (even another array); elements may be any mixture
# of objects of different types.
#
# Important data structures that use arrays include:
#
# * [Coordinate vector](https://en.wikipedia.org/wiki/Coordinate_vector).
# * [Matrix](https://en.wikipedia.org/wiki/Matrix_(mathematics)).
# * [Heap](https://en.wikipedia.org/wiki/Heap_(data_structure)).
# * [Hash table](https://en.wikipedia.org/wiki/Hash_table).
# * [Deque (double-ended
# queue)](https://en.wikipedia.org/wiki/Double-ended_queue).
# * [Queue](https://en.wikipedia.org/wiki/Queue_(abstract_data_type)).
# * [Stack](https://en.wikipedia.org/wiki/Stack_(abstract_data_type)).
#
# There are also array-like data structures:
#
# * [Associative array](https://en.wikipedia.org/wiki/Associative_array) (see
# Hash).
# * [Directory](https://en.wikipedia.org/wiki/Directory_(computing)) (see
# Dir).
# * [Environment](https://en.wikipedia.org/wiki/Environment_variable) (see
# ENV).
# * [Set](https://en.wikipedia.org/wiki/Set_(abstract_data_type)) (see Set).
# * [String](https://en.wikipedia.org/wiki/String_(computer_science)) (see
# String).
#
# ## Array Indexes
#
# Array indexing starts at 0, as in C or Java.
#
# A non-negative index is an offset from the first element:
#
# * Index 0 indicates the first element.
# * Index 1 indicates the second element.
# * ...
#
# A negative index is an offset, backwards, from the end of the array:
#
# * Index -1 indicates the last element.
# * Index -2 indicates the next-to-last element.
# * ...
#
# ### In-Range and Out-of-Range Indexes
#
# A non-negative index is *in range* if and only if it is smaller than the size
# of the array. For a 3-element array:
#
# * Indexes 0 through 2 are in range.
# * Index 3 is out of range.
#
# A negative index is *in range* if and only if its absolute value is not larger
# than the size of the array. For a 3-element array:
#
# * Indexes -1 through -3 are in range.
# * Index -4 is out of range.
#
# ### Effective Index
#
# Although the effective index into an array is always an integer, some methods
# (both within class Array and elsewhere) accept one or more non-integer
# arguments that are [integer-convertible
# objects](rdoc-ref:implicit_conversion.rdoc@Integer-Convertible+Objects).
#
# ## Creating Arrays
#
# You can create an Array object explicitly with:
#
# * An [array literal](rdoc-ref:syntax/literals.rdoc@Array+Literals):
#
# [1, 'one', :one, [2, 'two', :two]]
#
# * A [%w or %W string-array
# Literal](rdoc-ref:syntax/literals.rdoc@25w+and+-25W-3A+String-Array+Litera
# ls):
#
# %w[foo bar baz] # => ["foo", "bar", "baz"]
# %w[1 % *] # => ["1", "%", "*"]
#
# * A [%i or %I symbol-array
# Literal](rdoc-ref:syntax/literals.rdoc@25i+and+-25I-3A+Symbol-Array+Litera
# ls):
#
# %i[foo bar baz] # => [:foo, :bar, :baz]
# %i[1 % *] # => [:"1", :%, :*]
#
# * Method Kernel#Array:
#
# Array(["a", "b"]) # => ["a", "b"]
# Array(1..5) # => [1, 2, 3, 4, 5]
# Array(key: :value) # => [[:key, :value]]
# Array(nil) # => []
# Array(1) # => [1]
# Array({:a => "a", :b => "b"}) # => [[:a, "a"], [:b, "b"]]
#
# * Method Array.new:
#
# Array.new # => []
# Array.new(3) # => [nil, nil, nil]
# Array.new(4) {Hash.new} # => [{}, {}, {}, {}]
# Array.new(3, true) # => [true, true, true]
#
# Note that the last example above populates the array with references to
# the same object. This is recommended only in cases where that object is a
# natively immutable object such as a symbol, a numeric, `nil`, `true`, or
# `false`.
#
# Another way to create an array with various objects, using a block; this
# usage is safe for mutable objects such as hashes, strings or other arrays:
#
# Array.new(4) {|i| i.to_s } # => ["0", "1", "2", "3"]
#
# Here is a way to create a multi-dimensional array:
#
# Array.new(3) {Array.new(3)}
# # => [[nil, nil, nil], [nil, nil, nil], [nil, nil, nil]]
#
# A number of Ruby methods, both in the core and in the standard library,
# provide instance method `to_a`, which converts an object to an array.
#
# * ARGF#to_a
# * Array#to_a
# * Enumerable#to_a
# * Hash#to_a
# * MatchData#to_a
# * NilClass#to_a
# * OptionParser#to_a
# * Range#to_a
# * Set#to_a
# * Struct#to_a
# * Time#to_a
# * Benchmark::Tms#to_a
# * CSV::Table#to_a
# * Enumerator::Lazy#to_a
# * Gem::List#to_a
# * Gem::NameTuple#to_a
# * Gem::Platform#to_a
# * Gem::RequestSet::Lockfile::Tokenizer#to_a
# * Gem::SourceList#to_a
# * OpenSSL::X509::Extension#to_a
# * OpenSSL::X509::Name#to_a
# * Racc::ISet#to_a
# * Rinda::RingFinger#to_a
# * Ripper::Lexer::Elem#to_a
# * RubyVM::InstructionSequence#to_a
# * YAML::DBM#to_a
#
# ## Example Usage
#
# In addition to the methods it mixes in through the Enumerable module, class
# Array has proprietary methods for accessing, searching and otherwise
# manipulating arrays.
#
# Some of the more common ones are illustrated below.
#
# ## Accessing Elements
#
# Elements in an array can be retrieved using the Array#[] method. It can take
# a single integer argument (a numeric index), a pair of arguments (start and
# length) or a range. Negative indices start counting from the end, with -1
# being the last element.
#
# arr = [1, 2, 3, 4, 5, 6]
# arr[2] #=> 3
# arr[100] #=> nil
# arr[-3] #=> 4
# arr[2, 3] #=> [3, 4, 5]
# arr[1..4] #=> [2, 3, 4, 5]
# arr[1..-3] #=> [2, 3, 4]
#
# Another way to access a particular array element is by using the #at method
#
# arr.at(0) #=> 1
#
# The #slice method works in an identical manner to Array#[].
#
# To raise an error for indices outside of the array bounds or else to provide a
# default value when that happens, you can use #fetch.
#
# arr = ['a', 'b', 'c', 'd', 'e', 'f']
# arr.fetch(100) #=> IndexError: index 100 outside of array bounds: -6...6
# arr.fetch(100, "oops") #=> "oops"
#
# The special methods #first and #last will return the first and last elements
# of an array, respectively.
#
# arr.first #=> 1
# arr.last #=> 6
#
# To return the first `n` elements of an array, use #take
#
# arr.take(3) #=> [1, 2, 3]
#
# #drop does the opposite of #take, by returning the elements after `n` elements
# have been dropped:
#
# arr.drop(3) #=> [4, 5, 6]
#
# ## Obtaining Information about an Array
#
# An array keeps track of its own length at all times. To query an array about
# the number of elements it contains, use #length, #count or #size.
#
# browsers = ['Chrome', 'Firefox', 'Safari', 'Opera', 'IE']
# browsers.length #=> 5
# browsers.count #=> 5
#
# To check whether an array contains any elements at all
#
# browsers.empty? #=> false
#
# To check whether a particular item is included in the array
#
# browsers.include?('Konqueror') #=> false
#
# ## Adding Items to an Array
#
# Items can be added to the end of an array by using either #push or #<<
#
# arr = [1, 2, 3, 4]
# arr.push(5) #=> [1, 2, 3, 4, 5]
# arr << 6 #=> [1, 2, 3, 4, 5, 6]
#
# #unshift will add a new item to the beginning of an array.
#
# arr.unshift(0) #=> [0, 1, 2, 3, 4, 5, 6]
#
# With #insert you can add a new element to an array at any position.
#
# arr.insert(3, 'apple') #=> [0, 1, 2, 'apple', 3, 4, 5, 6]
#
# Using the #insert method, you can also insert multiple values at once:
#
# arr.insert(3, 'orange', 'pear', 'grapefruit')
# #=> [0, 1, 2, "orange", "pear", "grapefruit", "apple", 3, 4, 5, 6]
#
# ## Removing Items from an Array
#
# The method #pop removes the last element in an array and returns it:
#
# arr = [1, 2, 3, 4, 5, 6]
# arr.pop #=> 6
# arr #=> [1, 2, 3, 4, 5]
#
# To retrieve and at the same time remove the first item, use #shift:
#
# arr.shift #=> 1
# arr #=> [2, 3, 4, 5]
#
# To delete an element at a particular index:
#
# arr.delete_at(2) #=> 4
# arr #=> [2, 3, 5]
#
# To delete a particular element anywhere in an array, use #delete:
#
# arr = [1, 2, 2, 3]
# arr.delete(2) #=> 2
# arr #=> [1,3]
#
# A useful method if you need to remove `nil` values from an array is #compact:
#
# arr = ['foo', 0, nil, 'bar', 7, 'baz', nil]
# arr.compact #=> ['foo', 0, 'bar', 7, 'baz']
# arr #=> ['foo', 0, nil, 'bar', 7, 'baz', nil]
# arr.compact! #=> ['foo', 0, 'bar', 7, 'baz']
# arr #=> ['foo', 0, 'bar', 7, 'baz']
#
# Another common need is to remove duplicate elements from an array.
#
# It has the non-destructive #uniq, and destructive method #uniq!
#
# arr = [2, 5, 6, 556, 6, 6, 8, 9, 0, 123, 556]
# arr.uniq #=> [2, 5, 6, 556, 8, 9, 0, 123]
#
# ## Iterating over an Array
#
# Like all classes that include the Enumerable module, class Array has an each
# method, which defines what elements should be iterated over and how. In case
# of Array#each, all elements in `self` are yielded to the supplied block in
# sequence.
#
# Note that this operation leaves the array unchanged.
#
# arr = [1, 2, 3, 4, 5]
# arr.each {|a| print a -= 10, " "}
# # prints: -9 -8 -7 -6 -5
# #=> [1, 2, 3, 4, 5]
#
# Another sometimes useful iterator is #reverse_each which will iterate over the
# elements in the array in reverse order.
#
# words = %w[first second third fourth fifth sixth]
# str = ""
# words.reverse_each {|word| str += "#{word} "}
# p str #=> "sixth fifth fourth third second first "
#
# The #map method can be used to create a new array based on the original array,
# but with the values modified by the supplied block:
#
# arr.map {|a| 2*a} #=> [2, 4, 6, 8, 10]
# arr #=> [1, 2, 3, 4, 5]
# arr.map! {|a| a**2} #=> [1, 4, 9, 16, 25]
# arr #=> [1, 4, 9, 16, 25]
#
# ## Selecting Items from an Array
#
# Elements can be selected from an array according to criteria defined in a
# block. The selection can happen in a destructive or a non-destructive manner.
# While the destructive operations will modify the array they were called on,
# the non-destructive methods usually return a new array with the selected
# elements, but leave the original array unchanged.
#
# ### Non-destructive Selection
#
# arr = [1, 2, 3, 4, 5, 6]
# arr.select {|a| a > 3} #=> [4, 5, 6]
# arr.reject {|a| a < 3} #=> [3, 4, 5, 6]
# arr.drop_while {|a| a < 4} #=> [4, 5, 6]
# arr #=> [1, 2, 3, 4, 5, 6]
#
# ### Destructive Selection
#
# #select! and #reject! are the corresponding destructive methods to #select and
# #reject
#
# Similar to #select vs. #reject, #delete_if and #keep_if have the exact
# opposite result when supplied with the same block:
#
# arr.delete_if {|a| a < 4} #=> [4, 5, 6]
# arr #=> [4, 5, 6]
#
# arr = [1, 2, 3, 4, 5, 6]
# arr.keep_if {|a| a < 4} #=> [1, 2, 3]
# arr #=> [1, 2, 3]
#
# ## What's Here
#
# First, what's elsewhere. Class Array:
#
# * Inherits from [class Object](rdoc-ref:Object@What-27s+Here).
# * Includes [module Enumerable](rdoc-ref:Enumerable@What-27s+Here), which
# provides dozens of additional methods.
#
# Here, class Array provides methods that are useful for:
#
# * [Creating an Array](rdoc-ref:Array@Methods+for+Creating+an+Array)
# * [Querying](rdoc-ref:Array@Methods+for+Querying)
# * [Comparing](rdoc-ref:Array@Methods+for+Comparing)
# * [Fetching](rdoc-ref:Array@Methods+for+Fetching)
# * [Assigning](rdoc-ref:Array@Methods+for+Assigning)
# * [Deleting](rdoc-ref:Array@Methods+for+Deleting)
# * [Combining](rdoc-ref:Array@Methods+for+Combining)
# * [Iterating](rdoc-ref:Array@Methods+for+Iterating)
# * [Converting](rdoc-ref:Array@Methods+for+Converting)
# * [And more....](rdoc-ref:Array@Other+Methods)
#
# ### Methods for Creating an Array
#
# * ::[]: Returns a new array populated with given objects.
# * ::new: Returns a new array.
# * ::try_convert: Returns a new array created from a given object.
#
# See also [Creating Arrays](rdoc-ref:Array@Creating+Arrays).
#
# ### Methods for Querying
#
# * #all?: Returns whether all elements meet a given criterion.
# * #any?: Returns whether any element meets a given criterion.
# * #count: Returns the count of elements that meet a given criterion.
# * #empty?: Returns whether there are no elements.
# * #find_index (aliased as #index): Returns the index of the first element
# that meets a given criterion.
# * #hash: Returns the integer hash code.
# * #include?: Returns whether any element <code>==</code> a given object.
# * #length (aliased as #size): Returns the count of elements.
# * #none?: Returns whether no element <code>==</code> a given object.
# * #one?: Returns whether exactly one element <code>==</code> a given object.
# * #rindex: Returns the index of the last element that meets a given
# criterion.
#
# ### Methods for Comparing
#
# * #<=>: Returns -1, 0, or 1, as `self` is less than, equal to, or greater
# than a given object.
# * #==: Returns whether each element in `self` is <code>==</code> to the
# corresponding element in a given object.
# * #eql?: Returns whether each element in `self` is <code>eql?</code> to the
# corresponding element in a given object.
#
# ### Methods for Fetching
#
# These methods do not modify `self`.
#
# * #[] (aliased as #slice): Returns consecutive elements as determined by a
# given argument.
# * #assoc: Returns the first element that is an array whose first element
# <code>==</code> a given object.
# * #at: Returns the element at a given offset.
# * #bsearch: Returns an element selected via a binary search as determined by
# a given block.
# * #bsearch_index: Returns the index of an element selected via a binary
# search as determined by a given block.
# * #compact: Returns an array containing all non-`nil` elements.
# * #dig: Returns the object in nested objects that is specified by a given
# index and additional arguments.
# * #drop: Returns trailing elements as determined by a given index.
# * #drop_while: Returns trailing elements as determined by a given block.
# * #fetch: Returns the element at a given offset.
# * #fetch_values: Returns elements at given offsets.
# * #first: Returns one or more leading elements.
# * #last: Returns one or more trailing elements.
# * #max: Returns one or more maximum-valued elements, as determined by
# <code>#<=></code> or a given block.
# * #min: Returns one or more minimum-valued elements, as determined by
# <code>#<=></code> or a given block.
# * #minmax: Returns the minimum-valued and maximum-valued elements, as
# determined by <code>#<=></code> or a given block.
# * #rassoc: Returns the first element that is an array whose second element
# <code>==</code> a given object.
# * #reject: Returns an array containing elements not rejected by a given
# block.
# * #reverse: Returns all elements in reverse order.
# * #rotate: Returns all elements with some rotated from one end to the other.
# * #sample: Returns one or more random elements.
# * #select (aliased as #filter): Returns an array containing elements
# selected by a given block.
# * #shuffle: Returns elements in a random order.
# * #sort: Returns all elements in an order determined by <code>#<=></code> or
# a given block.
# * #take: Returns leading elements as determined by a given index.
# * #take_while: Returns leading elements as determined by a given block.
# * #uniq: Returns an array containing non-duplicate elements.
# * #values_at: Returns the elements at given offsets.
#
# ### Methods for Assigning
#
# These methods add, replace, or reorder elements in `self`.
#
# * #<<: Appends an element.
# * #[]=: Assigns specified elements with a given object.
# * #concat: Appends all elements from given arrays.
# * #fill: Replaces specified elements with specified objects.
# * #flatten!: Replaces each nested array in `self` with the elements from
# that array.
# * #initialize_copy (aliased as #replace): Replaces the content of `self`
# with the content of a given array.
# * #insert: Inserts given objects at a given offset; does not replace
# elements.
# * #push (aliased as #append): Appends elements.
# * #reverse!: Replaces `self` with its elements reversed.
# * #rotate!: Replaces `self` with its elements rotated.
# * #shuffle!: Replaces `self` with its elements in random order.
# * #sort!: Replaces `self` with its elements sorted, as determined by
# <code>#<=></code> or a given block.
# * #sort_by!: Replaces `self` with its elements sorted, as determined by a
# given block.
# * #unshift (aliased as #prepend): Prepends leading elements.
#
# ### Methods for Deleting
#
# Each of these methods removes elements from `self`:
#
# * #clear: Removes all elements.
# * #compact!: Removes all `nil` elements.
# * #delete: Removes elements equal to a given object.
# * #delete_at: Removes the element at a given offset.
# * #delete_if: Removes elements specified by a given block.
# * #keep_if: Removes elements not specified by a given block.
# * #pop: Removes and returns the last element.
# * #reject!: Removes elements specified by a given block.
# * #select! (aliased as #filter!): Removes elements not specified by a given
# block.
# * #shift: Removes and returns the first element.
# * #slice!: Removes and returns a sequence of elements.
# * #uniq!: Removes duplicates.
#
# ### Methods for Combining
#
# * #&: Returns an array containing elements found both in `self` and a given
# array.
# * #+: Returns an array containing all elements of `self` followed by all
# elements of a given array.
# * #-: Returns an array containing all elements of `self` that are not found
# in a given array.
# * #|: Returns an array containing all element of `self` and all elements of
# a given array, duplicates removed.
# * #difference: Returns an array containing all elements of `self` that are
# not found in any of the given arrays..
# * #intersection: Returns an array containing elements found both in `self`
# and in each given array.
# * #product: Returns or yields all combinations of elements from `self` and
# given arrays.
# * #reverse: Returns an array containing all elements of `self` in reverse
# order.
# * #union: Returns an array containing all elements of `self` and all
# elements of given arrays, duplicates removed.
#
# ### Methods for Iterating
#
# * #combination: Calls a given block with combinations of elements of `self`;
# a combination does not use the same element more than once.
# * #cycle: Calls a given block with each element, then does so again, for a
# specified number of times, or forever.
# * #each: Passes each element to a given block.
# * #each_index: Passes each element index to a given block.
# * #permutation: Calls a given block with permutations of elements of `self`;
# a permutation does not use the same element more than once.
# * #repeated_combination: Calls a given block with combinations of elements
# of `self`; a combination may use the same element more than once.
# * #repeated_permutation: Calls a given block with permutations of elements
# of `self`; a permutation may use the same element more than once.
# * #reverse_each: Passes each element, in reverse order, to a given block.
#
# ### Methods for Converting
#
# * #collect (aliased as #map): Returns an array containing the block
# return-value for each element.
# * #collect! (aliased as #map!): Replaces each element with a block
# return-value.
# * #flatten: Returns an array that is a recursive flattening of `self`.
# * #inspect (aliased as #to_s): Returns a new String containing the elements.
# * #join: Returns a new String containing the elements joined by the field
# separator.
# * #to_a: Returns `self` or a new array containing all elements.
# * #to_ary: Returns `self`.
# * #to_h: Returns a new hash formed from the elements.
# * #transpose: Transposes `self`, which must be an array of arrays.
# * #zip: Returns a new array of arrays containing `self` and given arrays.
#
# ### Other Methods
#
# * #*: Returns one of the following:
#
# * With integer argument `n`, a new array that is the concatenation of
# `n` copies of `self`.
# * With string argument `field_separator`, a new string that is
# equivalent to <code>join(field_separator)</code>.
#
# * #pack: Packs the elements into a binary sequence.
# * #sum: Returns a sum of elements according to either <code>+</code> or a
# given block.
#
%a{annotate:rdoc:source:from=array.c}
class Array[unchecked out E] < Object
include Enumerable[E]
# <!--
# rdoc-file=array.c
# - Array.new -> new_empty_array
# - Array.new(array) -> new_array
# - Array.new(size, default_value = nil) -> new_array
# - Array.new(size = 0) {|index| ... } -> new_array
# -->
# Returns a new array.
#
# With no block and no argument given, returns a new empty array:
#
# Array.new # => []
#
# With no block and array argument given, returns a new array with the same
# elements:
#
# Array.new([:foo, 'bar', 2]) # => [:foo, "bar", 2]
#
# With no block and integer argument given, returns a new array containing that
# many instances of the given `default_value`:
#
# Array.new(0) # => []
# Array.new(3) # => [nil, nil, nil]
# Array.new(2, 3) # => [3, 3]
#
# With a block given, returns an array of the given `size`; calls the block with
# each `index` in the range <code>(0...size)</code>; the element at that `index`
# in the returned array is the blocks return value:
#
# Array.new(3) {|index| "Element #{index}" } # => ["Element 0", "Element 1", "Element 2"]
#
# A common pitfall for new Rubyists is providing an expression as
# `default_value`:
#
# array = Array.new(2, {})
# array # => [{}, {}]
# array[0][:a] = 1
# array # => [{a: 1}, {a: 1}], as array[0] and array[1] are same object
#
# If you want the elements of the array to be distinct, you should pass a block:
#
# array = Array.new(2) { {} }
# array # => [{}, {}]
# array[0][:a] = 1
# array # => [{a: 1}, {}], as array[0] and array[1] are different objects
#
# Raises TypeError if the first argument is not either an array or an
# [integer-convertible
# object](rdoc-ref:implicit_conversion.rdoc@Integer-Convertible+Objects)).
# Raises ArgumentError if the first argument is a negative integer.
#
# Related: see [Methods for Creating an
# Array](rdoc-ref:Array@Methods+for+Creating+an+Array).
#
def initialize: () -> void
| (::Array[E] ary) -> void
| (int size, ?E val) -> void
| (int size) { (::Integer index) -> E } -> void
# <!--
# rdoc-file=array.c
# - [](*args)
# -->
# Returns a new array, populated with the given objects:
#
# Array[1, 'a', /^A/] # => [1, "a", /^A/]
# Array[] # => []
# Array.[](1, 'a', /^A/) # => [1, "a", /^A/]
#
# Related: see [Methods for Creating an
# Array](rdoc-ref:Array@Methods+for+Creating+an+Array).
#
def self.[]: [U] (*U) -> ::Array[U]
# <!--
# rdoc-file=array.c
# - Array.try_convert(object) -> object, new_array, or nil
# -->
# Attempts to return an array, based on the given `object`.
#
# If `object` is an array, returns `object`.
#
# Otherwise if `object` responds to <code>:to_ary</code>. calls
# <code>object.to_ary</code>: if the return value is an array or `nil`, returns
# that value; if not, raises TypeError.
#
# Otherwise returns `nil`.
#
# Related: see [Methods for Creating an
# Array](rdoc-ref:Array@Methods+for+Creating+an+Array).
#
def self.try_convert: [U] (untyped) -> ::Array[U]?
# <!--
# rdoc-file=array.c
# - self & other_array -> new_array
# -->
# Returns a new array containing the *intersection* of `self` and `other_array`;
# that is, containing those elements found in both `self` and `other_array`:
#
# [0, 1, 2, 3] & [1, 2] # => [1, 2]
#
# Omits duplicates:
#
# [0, 1, 1, 0] & [0, 1] # => [0, 1]
#
# Preserves order from `self`:
#
# [0, 1, 2] & [3, 2, 1, 0] # => [0, 1, 2]
#
# Identifies common elements using method <code>#eql?</code> (as defined in each
# element of `self`).
#
# Related: see [Methods for Combining](rdoc-ref:Array@Methods+for+Combining).
#
def &: (::Array[untyped] | _ToAry[untyped]) -> ::Array[E]
# <!--
# rdoc-file=array.c
# - self * n -> new_array
# - self * string_separator -> new_string
# -->
# When non-negative integer argument `n` is given, returns a new array built by
# concatenating `n` copies of `self`:
#
# a = ['x', 'y']
# a * 3 # => ["x", "y", "x", "y", "x", "y"]
#
# When string argument `string_separator` is given, equivalent to
# <code>self.join(string_separator)</code>:
#
# [0, [0, 1], {foo: 0}] * ', ' # => "0, 0, 1, {foo: 0}"
#
def *: (string str) -> ::String
| (int int) -> ::Array[E]
# <!--
# rdoc-file=array.c
# - self + other_array -> new_array
# -->
# Returns a new array containing all elements of `self` followed by all elements
# of `other_array`:
#
# a = [0, 1] + [2, 3]
# a # => [0, 1, 2, 3]
#
# Related: see [Methods for Combining](rdoc-ref:Array@Methods+for+Combining).
#
def +: [U] (_ToAry[U]) -> ::Array[E | U]
# <!--
# rdoc-file=array.c
# - self - other_array -> new_array
# -->
# Returns a new array containing only those elements of `self` that are not
# found in `other_array`; the order from `self` is preserved:
#
# [0, 1, 1, 2, 1, 1, 3, 1, 1] - [1] # => [0, 2, 3]
# [0, 1, 1, 2, 1, 1, 3, 1, 1] - [3, 2, 0, :foo] # => [1, 1, 1, 1, 1, 1]
# [0, 1, 2] - [:foo] # => [0, 1, 2]
#
# Element are compared using method <code>#eql?</code> (as defined in each
# element of `self`).
#
# Related: see [Methods for Combining](rdoc-ref:Array@Methods+for+Combining).
#
def -: (_ToAry[untyped]) -> ::Array[E]
# <!--
# rdoc-file=array.c
# - self << object -> self
# -->
# Appends `object` as the last element in `self`; returns `self`:
#
# [:foo, 'bar', 2] << :baz # => [:foo, "bar", 2, :baz]
#
# Appends `object` as a single element, even if it is another array:
#
# [:foo, 'bar', 2] << [3, 4] # => [:foo, "bar", 2, [3, 4]]
#
# Related: see [Methods for Assigning](rdoc-ref:Array@Methods+for+Assigning).
#
def <<: (E) -> self
# <!--
# rdoc-file=array.c
# - self <=> other_array -> -1, 0, or 1
# -->
# Returns -1, 0, or 1 as `self` is determined to be less than, equal to, or
# greater than `other_array`.
#
# Iterates over each index `i` in <code>(0...self.size)</code>:
#
# * Computes <code>result[i]</code> as <code>self[i] <=>
# other_array[i]</code>.
# * Immediately returns 1 if <code>result[i]</code> is 1:
#
# [0, 1, 2] <=> [0, 0, 2] # => 1
#
# * Immediately returns -1 if <code>result[i]</code> is -1:
#
# [0, 1, 2] <=> [0, 2, 2] # => -1
#
# * Continues if <code>result[i]</code> is 0.
#
# When every `result` is 0, returns <code>self.size <=> other_array.size</code>
# (see Integer#<=>):
#
# [0, 1, 2] <=> [0, 1] # => 1
# [0, 1, 2] <=> [0, 1, 2] # => 0
# [0, 1, 2] <=> [0, 1, 2, 3] # => -1
#
# Note that when `other_array` is larger than `self`, its trailing elements do
# not affect the result:
#
# [0, 1, 2] <=> [0, 1, 2, -3] # => -1
# [0, 1, 2] <=> [0, 1, 2, 0] # => -1
# [0, 1, 2] <=> [0, 1, 2, 3] # => -1
#
# Related: see [Methods for Comparing](rdoc-ref:Array@Methods+for+Comparing).
#
def <=>: (untyped) -> ::Integer?
# <!--
# rdoc-file=array.c
# - self == other_array -> true or false
# -->
# Returns whether both:
#
# * `self` and `other_array` are the same size.
# * Their corresponding elements are the same; that is, for each index `i` in
# <code>(0...self.size)</code>, <code>self[i] == other_array[i]</code>.
#
# Examples:
#
# [:foo, 'bar', 2] == [:foo, 'bar', 2] # => true
# [:foo, 'bar', 2] == [:foo, 'bar', 2.0] # => true
# [:foo, 'bar', 2] == [:foo, 'bar'] # => false # Different sizes.
# [:foo, 'bar', 2] == [:foo, 'bar', 3] # => false # Different elements.
#
# This method is different from method Array#eql?, which compares elements using
# <code>Object#eql?</code>.
#
# Related: see [Methods for Comparing](rdoc-ref:Array@Methods+for+Comparing).
#
def ==: (untyped other) -> bool
# <!--
# rdoc-file=array.c
# - self[index] -> object or nil
# - self[start, length] -> object or nil
# - self[range] -> object or nil
# - self[aseq] -> object or nil
# - slice(index) -> object or nil
# - slice(start, length) -> object or nil
# - slice(range) -> object or nil
# - slice(aseq) -> object or nil
# -->
# Returns elements from `self`; does not modify `self`.
#
# In brief:
#
# a = [:foo, 'bar', 2]
#
# # Single argument index: returns one element.
# a[0] # => :foo # Zero-based index.
# a[-1] # => 2 # Negative index counts backwards from end.
#
# # Arguments start and length: returns an array.
# a[1, 2] # => ["bar", 2]
# a[-2, 2] # => ["bar", 2] # Negative start counts backwards from end.
#
# # Single argument range: returns an array.
# a[0..1] # => [:foo, "bar"]
# a[0..-2] # => [:foo, "bar"] # Negative range-begin counts backwards from end.
# a[-2..2] # => ["bar", 2] # Negative range-end counts backwards from end.
#
# When a single integer argument `index` is given, returns the element at offset
# `index`:
#
# a = [:foo, 'bar', 2]
# a[0] # => :foo
# a[2] # => 2
# a # => [:foo, "bar", 2]
#
# If `index` is negative, counts backwards from the end of `self`:
#
# a = [:foo, 'bar', 2]
# a[-1] # => 2
# a[-2] # => "bar"
#
# If `index` is out of range, returns `nil`.
#
# When two Integer arguments `start` and `length` are given, returns a new array
# of size `length` containing successive elements beginning at offset `start`:
#
# a = [:foo, 'bar', 2]
# a[0, 2] # => [:foo, "bar"]
# a[1, 2] # => ["bar", 2]
#
# If <code>start + length</code> is greater than <code>self.length</code>,
# returns all elements from offset `start` to the end:
#
# a = [:foo, 'bar', 2]
# a[0, 4] # => [:foo, "bar", 2]
# a[1, 3] # => ["bar", 2]
# a[2, 2] # => [2]
#
# If <code>start == self.size</code> and <code>length >= 0</code>, returns a new
# empty array.
#
# If `length` is negative, returns `nil`.
#
# When a single Range argument `range` is given, treats <code>range.min</code>
# as `start` above and <code>range.size</code> as `length` above:
#
# a = [:foo, 'bar', 2]
# a[0..1] # => [:foo, "bar"]
# a[1..2] # => ["bar", 2]
#
# Special case: If <code>range.start == a.size</code>, returns a new empty
# array.
#
# If <code>range.end</code> is negative, calculates the end index from the end:
#
# a = [:foo, 'bar', 2]
# a[0..-1] # => [:foo, "bar", 2]
# a[0..-2] # => [:foo, "bar"]
# a[0..-3] # => [:foo]
#
# If <code>range.start</code> is negative, calculates the start index from the
# end:
#
# a = [:foo, 'bar', 2]
# a[-1..2] # => [2]
# a[-2..2] # => ["bar", 2]
# a[-3..2] # => [:foo, "bar", 2]
#
# If <code>range.start</code> is larger than the array size, returns `nil`.
#
# a = [:foo, 'bar', 2]
# a[4..1] # => nil
# a[4..0] # => nil
# a[4..-1] # => nil
#
# When a single Enumerator::ArithmeticSequence argument `aseq` is given, returns
# an array of elements corresponding to the indexes produced by the sequence.
#
# a = ['--', 'data1', '--', 'data2', '--', 'data3']
# a[(1..).step(2)] # => ["data1", "data2", "data3"]
#
# Unlike slicing with range, if the start or the end of the arithmetic sequence
# is larger than array size, throws RangeError.
#
# a = ['--', 'data1', '--', 'data2', '--', 'data3']
# a[(1..11).step(2)]
# # RangeError (((1..11).step(2)) out of range)
# a[(7..).step(2)]
# # RangeError (((7..).step(2)) out of range)
#
# If given a single argument, and its type is not one of the listed, tries to
# convert it to Integer, and raises if it is impossible:
#
# a = [:foo, 'bar', 2]
# # Raises TypeError (no implicit conversion of Symbol into Integer):
# a[:foo]
#
# Related: see [Methods for Fetching](rdoc-ref:Array@Methods+for+Fetching).
#
def []: %a{implicitly-returns-nil} (int index) -> E
| (int start, int length) -> ::Array[E]?
| (::Range[::Integer?] range) -> ::Array[E]?
# <!--
# rdoc-file=array.c
# - self[index] = object -> object
# - self[start, length] = object -> object
# - self[range] = object -> object
# -->
# Assigns elements in `self`, based on the given `object`; returns `object`.
#
# In brief:
#
# a_orig = [:foo, 'bar', 2]
#
# # With argument index.
# a = a_orig.dup
# a[0] = 'foo' # => "foo"
# a # => ["foo", "bar", 2]
# a = a_orig.dup
# a[7] = 'foo' # => "foo"
# a # => [:foo, "bar", 2, nil, nil, nil, nil, "foo"]
#
# # With arguments start and length.
# a = a_orig.dup
# a[0, 2] = 'foo' # => "foo"
# a # => ["foo", 2]
# a = a_orig.dup
# a[6, 50] = 'foo' # => "foo"
# a # => [:foo, "bar", 2, nil, nil, nil, "foo"]
#
# # With argument range.
# a = a_orig.dup
# a[0..1] = 'foo' # => "foo"
# a # => ["foo", 2]
# a = a_orig.dup
# a[6..50] = 'foo' # => "foo"
# a # => [:foo, "bar", 2, nil, nil, nil, "foo"]
#
# When Integer argument `index` is given, assigns `object` to an element in
# `self`.
#
# If `index` is non-negative, assigns `object` the element at offset `index`:
#
# a = [:foo, 'bar', 2]
# a[0] = 'foo' # => "foo"
# a # => ["foo", "bar", 2]
#
# If `index` is greater than <code>self.length</code>, extends the array:
#
# a = [:foo, 'bar', 2]
# a[7] = 'foo' # => "foo"
# a # => [:foo, "bar", 2, nil, nil, nil, nil, "foo"]
#
# If `index` is negative, counts backwards from the end of the array:
#
# a = [:foo, 'bar', 2]
# a[-1] = 'two' # => "two"
# a # => [:foo, "bar", "two"]
#
# When Integer arguments `start` and `length` are given and `object` is not an
# array, removes <code>length - 1</code> elements beginning at offset `start`,
# and assigns `object` at offset `start`:
#
# a = [:foo, 'bar', 2]
# a[0, 2] = 'foo' # => "foo"
# a # => ["foo", 2]
#
# If `start` is negative, counts backwards from the end of the array:
#
# a = [:foo, 'bar', 2]
# a[-2, 2] = 'foo' # => "foo"
# a # => [:foo, "foo"]
#
# If `start` is non-negative and outside the array (<code> >= self.size</code>),
# extends the array with `nil`, assigns `object` at offset `start`, and ignores
# `length`:
#
# a = [:foo, 'bar', 2]