rbs 2.0.0 → 2.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/comments.yml +34 -0
- data/.github/workflows/ruby.yml +5 -0
- data/CHANGELOG.md +82 -0
- data/README.md +6 -1
- data/Rakefile +56 -21
- data/core/array.rbs +2866 -1086
- data/core/basic_object.rbs +150 -30
- data/core/binding.rbs +33 -0
- data/core/builtin.rbs +4 -4
- data/core/class.rbs +43 -5
- data/core/comparable.rbs +57 -0
- data/core/complex.rbs +170 -4
- data/core/constants.rbs +51 -0
- data/core/deprecated.rbs +7 -0
- data/core/dir.rbs +305 -20
- data/core/encoding.rbs +1214 -77
- data/core/enumerable.rbs +2173 -234
- data/core/enumerator.rbs +448 -182
- data/core/env.rbs +448 -1
- data/core/errno.rbs +1 -10
- data/core/errors.rbs +152 -2
- data/core/exception.rbs +201 -127
- data/core/false_class.rbs +27 -0
- data/core/fiber.rbs +118 -37
- data/core/fiber_error.rbs +8 -9
- data/core/file.rbs +1060 -142
- data/core/file_test.rbs +287 -32
- data/core/float.rbs +776 -300
- data/core/gc.rbs +185 -34
- data/core/global_variables.rbs +5 -1
- data/core/hash.rbs +1582 -649
- data/core/integer.rbs +974 -204
- data/core/io/buffer.rbs +710 -0
- data/core/io/wait.rbs +29 -8
- data/core/io.rbs +2438 -417
- data/core/kernel.rbs +2319 -318
- data/core/marshal.rbs +37 -2
- data/core/match_data.rbs +123 -6
- data/core/math.rbs +126 -6
- data/core/method.rbs +226 -102
- data/core/module.rbs +421 -45
- data/core/nil_class.rbs +64 -0
- data/core/numeric.rbs +620 -142
- data/core/object.rbs +453 -81
- data/core/object_space.rbs +92 -2
- data/core/proc.rbs +482 -285
- data/core/process.rbs +443 -34
- data/core/ractor.rbs +232 -9
- data/core/random.rbs +151 -52
- data/core/range.rbs +885 -160
- data/core/rational.rbs +122 -6
- data/core/rb_config.rbs +14 -4
- data/core/refinement.rbs +44 -0
- data/core/regexp.rbs +156 -14
- data/core/ruby_vm.rbs +42 -3
- data/core/signal.rbs +78 -39
- data/core/string.rbs +2123 -567
- data/core/string_io.rbs +204 -0
- data/core/struct.rbs +283 -28
- data/core/symbol.rbs +304 -30
- data/core/thread.rbs +1288 -688
- data/core/thread_group.rbs +66 -10
- data/core/time.rbs +643 -217
- data/core/trace_point.rbs +100 -12
- data/core/true_class.rbs +24 -0
- data/core/unbound_method.rbs +73 -7
- data/core/warning.rbs +37 -12
- data/docs/CONTRIBUTING.md +40 -34
- data/docs/stdlib.md +3 -102
- data/docs/syntax.md +54 -11
- data/ext/rbs_extension/extconf.rb +1 -0
- data/ext/rbs_extension/lexer.h +5 -0
- data/ext/rbs_extension/lexstate.c +6 -0
- data/ext/rbs_extension/parser.c +85 -10
- data/ext/rbs_extension/ruby_objs.c +4 -2
- data/ext/rbs_extension/ruby_objs.h +2 -2
- data/goodcheck.yml +0 -11
- data/lib/rbs/annotate/annotations.rb +197 -0
- data/lib/rbs/annotate/formatter.rb +80 -0
- data/lib/rbs/annotate/rdoc_annotator.rb +398 -0
- data/lib/rbs/annotate/rdoc_source.rb +120 -0
- data/lib/rbs/annotate.rb +6 -0
- data/lib/rbs/ast/members.rb +21 -13
- data/lib/rbs/buffer.rb +17 -11
- data/lib/rbs/cli.rb +48 -1
- data/lib/rbs/definition_builder/method_builder.rb +28 -16
- data/lib/rbs/definition_builder.rb +6 -2
- data/lib/rbs/environment.rb +8 -4
- data/lib/rbs/location_aux.rb +12 -0
- data/lib/rbs/namespace.rb +1 -1
- data/lib/rbs/prototype/rb.rb +12 -0
- data/lib/rbs/type_alias_regularity.rb +6 -4
- data/lib/rbs/type_name.rb +1 -1
- data/lib/rbs/types.rb +1 -1
- data/lib/rbs/validator.rb +6 -0
- data/lib/rbs/version.rb +1 -1
- data/lib/rbs/writer.rb +54 -4
- data/lib/rbs.rb +0 -2
- data/schema/typeParam.json +3 -3
- data/sig/annotate/annotations.rbs +102 -0
- data/sig/annotate/formatter.rbs +24 -0
- data/sig/annotate/rdoc_annotater.rbs +82 -0
- data/sig/annotate/rdoc_source.rbs +30 -0
- data/sig/buffer.rbs +6 -2
- data/sig/cli.rbs +2 -0
- data/sig/collection/{collections.rbs → sources.rbs} +0 -0
- data/sig/location.rbs +6 -0
- data/sig/members.rbs +24 -18
- data/sig/method_builder.rbs +5 -4
- data/sig/method_types.rbs +5 -1
- data/sig/polyfill.rbs +78 -0
- data/sig/validator.rbs +3 -1
- data/sig/writer.rbs +79 -2
- data/stdlib/abbrev/0/abbrev.rbs +6 -0
- data/stdlib/abbrev/0/array.rbs +26 -0
- data/stdlib/base64/0/base64.rbs +31 -0
- data/stdlib/benchmark/0/benchmark.rbs +74 -3
- data/stdlib/bigdecimal/0/big_decimal.rbs +614 -165
- data/stdlib/bigdecimal-math/0/big_math.rbs +41 -64
- data/stdlib/cgi/0/core.rbs +649 -21
- data/stdlib/coverage/0/coverage.rbs +164 -2
- data/stdlib/csv/0/csv.rbs +2862 -398
- data/stdlib/date/0/date.rbs +483 -25
- data/stdlib/date/0/date_time.rbs +187 -12
- data/stdlib/dbm/0/dbm.rbs +152 -17
- data/stdlib/digest/0/digest.rbs +146 -0
- data/stdlib/erb/0/erb.rbs +65 -245
- data/stdlib/fiber/0/fiber.rbs +73 -91
- data/stdlib/fileutils/0/fileutils.rbs +301 -1
- data/stdlib/find/0/find.rbs +9 -0
- data/stdlib/forwardable/0/forwardable.rbs +65 -1
- data/stdlib/io-console/0/io-console.rbs +227 -15
- data/stdlib/ipaddr/0/ipaddr.rbs +161 -0
- data/stdlib/json/0/json.rbs +1147 -145
- data/stdlib/logger/0/formatter.rbs +24 -0
- data/stdlib/logger/0/log_device.rbs +64 -0
- data/stdlib/logger/0/logger.rbs +165 -13
- data/stdlib/logger/0/period.rbs +10 -0
- data/stdlib/logger/0/severity.rbs +26 -0
- data/stdlib/monitor/0/monitor.rbs +163 -0
- data/stdlib/mutex_m/0/mutex_m.rbs +35 -6
- data/stdlib/net-http/0/manifest.yaml +1 -0
- data/stdlib/net-http/0/net-http.rbs +1513 -683
- data/stdlib/nkf/0/nkf.rbs +372 -0
- data/stdlib/objspace/0/objspace.rbs +149 -90
- data/stdlib/openssl/0/openssl.rbs +8108 -71
- data/stdlib/optparse/0/optparse.rbs +487 -19
- data/stdlib/pathname/0/pathname.rbs +425 -124
- data/stdlib/prettyprint/0/prettyprint.rbs +120 -99
- data/stdlib/prime/0/integer-extension.rbs +20 -2
- data/stdlib/prime/0/prime.rbs +88 -21
- data/stdlib/pstore/0/pstore.rbs +102 -0
- data/stdlib/pty/0/pty.rbs +64 -14
- data/stdlib/resolv/0/resolv.rbs +420 -31
- data/stdlib/rubygems/0/basic_specification.rbs +4 -1
- data/stdlib/rubygems/0/config_file.rbs +33 -1
- data/stdlib/rubygems/0/dependency_installer.rbs +4 -3
- data/stdlib/rubygems/0/installer.rbs +13 -1
- data/stdlib/rubygems/0/path_support.rbs +4 -1
- data/stdlib/rubygems/0/platform.rbs +5 -1
- data/stdlib/rubygems/0/request_set.rbs +44 -2
- data/stdlib/rubygems/0/requirement.rbs +65 -2
- data/stdlib/rubygems/0/rubygems.rbs +407 -0
- data/stdlib/rubygems/0/source_list.rbs +13 -0
- data/stdlib/rubygems/0/specification.rbs +21 -1
- data/stdlib/rubygems/0/stream_ui.rbs +3 -1
- data/stdlib/rubygems/0/uninstaller.rbs +8 -1
- data/stdlib/rubygems/0/version.rbs +60 -157
- data/stdlib/securerandom/0/securerandom.rbs +44 -0
- data/stdlib/set/0/set.rbs +423 -109
- data/stdlib/shellwords/0/shellwords.rbs +55 -77
- data/stdlib/singleton/0/singleton.rbs +20 -0
- data/stdlib/socket/0/addrinfo.rbs +210 -9
- data/stdlib/socket/0/basic_socket.rbs +103 -11
- data/stdlib/socket/0/ip_socket.rbs +31 -9
- data/stdlib/socket/0/socket.rbs +586 -38
- data/stdlib/socket/0/tcp_server.rbs +22 -2
- data/stdlib/socket/0/tcp_socket.rbs +12 -1
- data/stdlib/socket/0/udp_socket.rbs +25 -2
- data/stdlib/socket/0/unix_server.rbs +22 -2
- data/stdlib/socket/0/unix_socket.rbs +45 -5
- data/stdlib/strscan/0/string_scanner.rbs +210 -9
- data/stdlib/tempfile/0/tempfile.rbs +58 -10
- data/stdlib/time/0/time.rbs +208 -116
- data/stdlib/timeout/0/timeout.rbs +10 -0
- data/stdlib/tmpdir/0/tmpdir.rbs +13 -4
- data/stdlib/tsort/0/cyclic.rbs +1 -0
- data/stdlib/tsort/0/interfaces.rbs +1 -0
- data/stdlib/tsort/0/tsort.rbs +42 -0
- data/stdlib/uri/0/common.rbs +57 -8
- data/stdlib/uri/0/file.rbs +55 -109
- data/stdlib/uri/0/ftp.rbs +6 -3
- data/stdlib/uri/0/generic.rbs +558 -329
- data/stdlib/uri/0/http.rbs +60 -114
- data/stdlib/uri/0/https.rbs +8 -102
- data/stdlib/uri/0/ldap.rbs +143 -137
- data/stdlib/uri/0/ldaps.rbs +8 -102
- data/stdlib/uri/0/mailto.rbs +3 -0
- data/stdlib/uri/0/rfc2396_parser.rbs +66 -26
- data/stdlib/uri/0/ws.rbs +6 -3
- data/stdlib/uri/0/wss.rbs +5 -3
- data/stdlib/yaml/0/dbm.rbs +151 -87
- data/stdlib/yaml/0/store.rbs +6 -0
- data/stdlib/zlib/0/zlib.rbs +90 -31
- metadata +18 -6
- data/lib/rbs/location.rb +0 -221
- data/sig/char_scanner.rbs +0 -9
data/core/array.rbs
CHANGED
|
@@ -1,21 +1,54 @@
|
|
|
1
|
-
#
|
|
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
|
-
#
|
|
11
|
-
#
|
|
12
|
-
#
|
|
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
|
|
17
|
-
#
|
|
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
|
-
#
|
|
240
|
-
#
|
|
241
|
-
#
|
|
242
|
-
#
|
|
243
|
-
#
|
|
244
|
-
#
|
|
245
|
-
#
|
|
246
|
-
#
|
|
247
|
-
#
|
|
248
|
-
#
|
|
249
|
-
#
|
|
250
|
-
#
|
|
251
|
-
#
|
|
252
|
-
#
|
|
253
|
-
#
|
|
254
|
-
#
|
|
255
|
-
#
|
|
256
|
-
#
|
|
257
|
-
#
|
|
258
|
-
#
|
|
259
|
-
# Array.new(3)
|
|
260
|
-
# # => [
|
|
261
|
-
#
|
|
262
|
-
#
|
|
263
|
-
#
|
|
264
|
-
#
|
|
265
|
-
#
|
|
266
|
-
#
|
|
267
|
-
#
|
|
268
|
-
#
|
|
269
|
-
#
|
|
270
|
-
# a
|
|
271
|
-
# a # => [
|
|
272
|
-
#
|
|
273
|
-
#
|
|
274
|
-
#
|
|
275
|
-
#
|
|
276
|
-
#
|
|
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
|
-
#
|
|
301
|
-
#
|
|
302
|
-
#
|
|
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
|
-
#
|
|
305
|
-
# Array.try_convert("1") #=> nil
|
|
634
|
+
# Returns `nil` if `object` does not respond to `:to_ary`
|
|
306
635
|
#
|
|
307
|
-
#
|
|
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
|
-
#
|
|
318
|
-
#
|
|
319
|
-
#
|
|
320
|
-
#
|
|
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
|
-
#
|
|
323
|
-
# [
|
|
651
|
+
# Preserves order from `array`:
|
|
652
|
+
# [0, 1, 2] & [3, 2, 1, 0] # => [0, 1, 2]
|
|
324
653
|
#
|
|
325
|
-
#
|
|
654
|
+
# Related: Array#intersection.
|
|
326
655
|
#
|
|
327
656
|
def &: (::Array[untyped] | _ToAry[untyped]) -> ::Array[Elem]
|
|
328
657
|
|
|
329
|
-
#
|
|
330
|
-
#
|
|
331
|
-
#
|
|
332
|
-
#
|
|
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
|
-
#
|
|
335
|
-
#
|
|
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
|
-
#
|
|
341
|
-
#
|
|
342
|
-
#
|
|
343
|
-
#
|
|
344
|
-
#
|
|
345
|
-
#
|
|
346
|
-
#
|
|
347
|
-
# a
|
|
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
|
-
#
|
|
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
|
-
#
|
|
363
|
-
#
|
|
364
|
-
#
|
|
365
|
-
#
|
|
366
|
-
#
|
|
367
|
-
#
|
|
368
|
-
#
|
|
369
|
-
#
|
|
370
|
-
# [
|
|
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
|
-
#
|
|
377
|
-
#
|
|
378
|
-
# See also Array#difference.
|
|
699
|
+
# Related: Array#difference.
|
|
379
700
|
#
|
|
380
701
|
def -: (_ToAry[untyped]) -> ::Array[Elem]
|
|
381
702
|
|
|
382
|
-
#
|
|
383
|
-
#
|
|
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
|
-
#
|
|
386
|
-
# a
|
|
387
|
-
#
|
|
388
|
-
#
|
|
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
|
-
#
|
|
394
|
-
#
|
|
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
|
-
#
|
|
726
|
+
# Returns -1 if any result is -1:
|
|
727
|
+
# [0, 1, 2] <=> [0, 1, 3] # => -1
|
|
397
728
|
#
|
|
398
|
-
#
|
|
399
|
-
#
|
|
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
|
-
#
|
|
405
|
-
#
|
|
406
|
-
#
|
|
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
|
-
#
|
|
410
|
-
#
|
|
736
|
+
# * Returns 1 if `array` is larger than `other_array`:
|
|
737
|
+
# [0, 1, 2] <=> [0, 1] # => 1
|
|
411
738
|
#
|
|
412
|
-
#
|
|
413
|
-
#
|
|
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
|
-
#
|
|
419
|
-
#
|
|
420
|
-
#
|
|
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
|
-
#
|
|
423
|
-
#
|
|
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
|
-
#
|
|
429
|
-
#
|
|
430
|
-
#
|
|
431
|
-
#
|
|
432
|
-
#
|
|
433
|
-
#
|
|
434
|
-
#
|
|
435
|
-
#
|
|
436
|
-
#
|
|
437
|
-
#
|
|
438
|
-
#
|
|
439
|
-
#
|
|
440
|
-
#
|
|
441
|
-
#
|
|
442
|
-
#
|
|
443
|
-
# a
|
|
444
|
-
# a[
|
|
445
|
-
# a[
|
|
446
|
-
# a
|
|
447
|
-
#
|
|
448
|
-
#
|
|
449
|
-
# a[
|
|
450
|
-
# a[
|
|
451
|
-
# a[
|
|
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
|
-
#
|
|
458
|
-
#
|
|
459
|
-
#
|
|
460
|
-
#
|
|
461
|
-
#
|
|
462
|
-
#
|
|
463
|
-
#
|
|
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
|
-
#
|
|
865
|
+
# When Integer argument `index` is given, assigns `object` to an element in
|
|
866
|
+
# `self`.
|
|
472
867
|
#
|
|
473
|
-
#
|
|
474
|
-
# a
|
|
475
|
-
# a[0
|
|
476
|
-
# a
|
|
477
|
-
#
|
|
478
|
-
#
|
|
479
|
-
# a
|
|
480
|
-
# a[
|
|
481
|
-
# a
|
|
482
|
-
#
|
|
483
|
-
#
|
|
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
|
-
#
|
|
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
|
-
#
|
|
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
|
-
|
|
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
|
-
#
|
|
509
|
-
#
|
|
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
|
-
#
|
|
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
|
-
#
|
|
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
|
-
|
|
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
|
-
|
|
526
|
-
# a.at(0) #=> "a"
|
|
527
|
-
# a.at(-1) #=> "e"
|
|
528
|
-
#
|
|
529
|
-
def at: (int index) -> Elem?
|
|
1046
|
+
alias append push
|
|
530
1047
|
|
|
531
|
-
#
|
|
532
|
-
#
|
|
533
|
-
#
|
|
534
|
-
#
|
|
535
|
-
#
|
|
536
|
-
#
|
|
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
|
-
#
|
|
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
|
-
#
|
|
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
|
-
|
|
548
|
-
|
|
549
|
-
#
|
|
550
|
-
#
|
|
551
|
-
#
|
|
552
|
-
#
|
|
553
|
-
#
|
|
554
|
-
#
|
|
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
|
-
#
|
|
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
|
-
#
|
|
584
|
-
#
|
|
585
|
-
#
|
|
586
|
-
#
|
|
587
|
-
#
|
|
588
|
-
#
|
|
589
|
-
#
|
|
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
|
-
#
|
|
595
|
-
#
|
|
596
|
-
#
|
|
597
|
-
#
|
|
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
|
-
#
|
|
602
|
-
#
|
|
603
|
-
#
|
|
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
|
-
#
|
|
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
|
-
#
|
|
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
|
-
#
|
|
618
|
-
#
|
|
619
|
-
#
|
|
620
|
-
#
|
|
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
|
-
#
|
|
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
|
-
#
|
|
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
|
-
#
|
|
635
|
-
#
|
|
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
|
-
#
|
|
638
|
-
#
|
|
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
|
-
#
|
|
1161
|
+
# Example:
|
|
1162
|
+
# a = [0, 1, 2]
|
|
1163
|
+
# a.combination(2) {|combination| p combination }
|
|
641
1164
|
#
|
|
642
|
-
#
|
|
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
|
-
#
|
|
645
|
-
# a
|
|
646
|
-
# a.combination(
|
|
647
|
-
#
|
|
648
|
-
#
|
|
649
|
-
#
|
|
650
|
-
#
|
|
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
|
-
#
|
|
656
|
-
#
|
|
657
|
-
#
|
|
658
|
-
#
|
|
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
|
-
#
|
|
663
|
-
#
|
|
664
|
-
#
|
|
1207
|
+
# <!--
|
|
1208
|
+
# rdoc-file=array.c
|
|
1209
|
+
# - array.compact! -> self or nil
|
|
1210
|
+
# -->
|
|
1211
|
+
# Removes all `nil` elements from `self`.
|
|
665
1212
|
#
|
|
666
|
-
#
|
|
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
|
-
#
|
|
672
|
-
#
|
|
673
|
-
#
|
|
674
|
-
#
|
|
675
|
-
#
|
|
676
|
-
#
|
|
677
|
-
# a = [ 1
|
|
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
|
-
#
|
|
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
|
-
#
|
|
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
|
-
#
|
|
694
|
-
#
|
|
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
|
-
#
|
|
697
|
-
#
|
|
698
|
-
#
|
|
699
|
-
#
|
|
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
|
-
#
|
|
706
|
-
#
|
|
707
|
-
#
|
|
708
|
-
#
|
|
709
|
-
#
|
|
710
|
-
#
|
|
711
|
-
#
|
|
712
|
-
#
|
|
713
|
-
#
|
|
714
|
-
#
|
|
715
|
-
#
|
|
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
|
-
#
|
|
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
|
|
1308
|
+
# Returns `nil` if no elements removed.
|
|
725
1309
|
#
|
|
726
|
-
#
|
|
727
|
-
#
|
|
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
|
-
#
|
|
731
|
-
#
|
|
732
|
-
#
|
|
733
|
-
# a
|
|
734
|
-
# a.delete(
|
|
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
|
-
#
|
|
740
|
-
#
|
|
741
|
-
#
|
|
742
|
-
#
|
|
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
|
-
#
|
|
745
|
-
# a
|
|
746
|
-
# a
|
|
747
|
-
# a
|
|
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
|
-
|
|
750
|
-
|
|
751
|
-
# Deletes every element of `self` for which block evaluates to `true`.
|
|
1338
|
+
# If index is too large, returns `nil`.
|
|
752
1339
|
#
|
|
753
|
-
#
|
|
754
|
-
#
|
|
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
|
-
#
|
|
1345
|
+
# If `index` is too small (far from zero), returns nil.
|
|
757
1346
|
#
|
|
758
|
-
|
|
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
|
-
#
|
|
761
|
-
#
|
|
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
|
-
#
|
|
767
|
-
#
|
|
768
|
-
#
|
|
769
|
-
#
|
|
770
|
-
#
|
|
771
|
-
#
|
|
772
|
-
#
|
|
773
|
-
#
|
|
774
|
-
# [
|
|
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
|
-
#
|
|
1377
|
+
# Returns a copy of `self` if no arguments given.
|
|
787
1378
|
#
|
|
788
|
-
#
|
|
1379
|
+
# Related: Array#-.
|
|
789
1380
|
#
|
|
790
1381
|
def difference: (*::Array[untyped] arrays) -> ::Array[Elem]
|
|
791
1382
|
|
|
792
|
-
#
|
|
793
|
-
#
|
|
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
|
-
#
|
|
796
|
-
#
|
|
797
|
-
# a.dig(
|
|
798
|
-
# a.dig(1, 2,
|
|
799
|
-
# a.dig(
|
|
800
|
-
#
|
|
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
|
-
#
|
|
806
|
-
#
|
|
807
|
-
#
|
|
808
|
-
#
|
|
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
|
-
#
|
|
811
|
-
#
|
|
812
|
-
# a
|
|
813
|
-
# a.drop(
|
|
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
|
-
#
|
|
818
|
-
#
|
|
819
|
-
#
|
|
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
|
-
#
|
|
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
|
-
#
|
|
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
|
-
#
|
|
832
|
-
#
|
|
1436
|
+
# <!--
|
|
1437
|
+
# rdoc-file=array.c
|
|
1438
|
+
# - array.each {|element| ... } -> self
|
|
1439
|
+
# - array.each -> Enumerator
|
|
1440
|
+
# -->
|
|
1441
|
+
# Iterates over array elements.
|
|
833
1442
|
#
|
|
834
|
-
#
|
|
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
|
-
#
|
|
837
|
-
#
|
|
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
|
-
#
|
|
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
|
-
#
|
|
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
|
-
#
|
|
847
|
-
#
|
|
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
|
-
#
|
|
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
|
-
#
|
|
852
|
-
#
|
|
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
|
-
#
|
|
1508
|
+
# Output:
|
|
1509
|
+
# 0 foo
|
|
1510
|
+
# 1 bar
|
|
1511
|
+
# 2 2
|
|
855
1512
|
#
|
|
856
|
-
#
|
|
1513
|
+
# Related: #each, #reverse_each.
|
|
857
1514
|
#
|
|
858
1515
|
def each_index: () { (::Integer index) -> void } -> self
|
|
859
1516
|
| () -> ::Enumerator[::Integer, self]
|
|
860
1517
|
|
|
861
|
-
#
|
|
862
|
-
#
|
|
863
|
-
#
|
|
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
|
-
#
|
|
868
|
-
#
|
|
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
|
-
|
|
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
|
-
#
|
|
878
|
-
#
|
|
1538
|
+
# This method is different from method [Array#==](#method-i-3D-3D), which
|
|
1539
|
+
# compares using method `Object#==`.
|
|
879
1540
|
#
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
#
|
|
883
|
-
#
|
|
884
|
-
#
|
|
885
|
-
#
|
|
886
|
-
#
|
|
887
|
-
#
|
|
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
|
-
#
|
|
894
|
-
#
|
|
895
|
-
#
|
|
896
|
-
#
|
|
897
|
-
#
|
|
898
|
-
#
|
|
899
|
-
#
|
|
900
|
-
#
|
|
901
|
-
#
|
|
902
|
-
#
|
|
903
|
-
#
|
|
904
|
-
#
|
|
905
|
-
#
|
|
906
|
-
#
|
|
907
|
-
#
|
|
908
|
-
# a
|
|
909
|
-
# a
|
|
910
|
-
# a.fill
|
|
911
|
-
#
|
|
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
|
-
#
|
|
920
|
-
#
|
|
921
|
-
#
|
|
922
|
-
#
|
|
923
|
-
#
|
|
924
|
-
#
|
|
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
|
-
#
|
|
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
|
-
#
|
|
937
|
-
#
|
|
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
|
-
#
|
|
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
|
-
#
|
|
1780
|
+
# Returns `nil` if no elements were removed.
|
|
942
1781
|
#
|
|
943
|
-
#
|
|
944
|
-
#
|
|
945
|
-
#
|
|
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
|
-
#
|
|
953
|
-
#
|
|
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
|
-
#
|
|
956
|
-
#
|
|
957
|
-
#
|
|
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
|
-
#
|
|
1804
|
+
# Returns `nil` if no such element found.
|
|
960
1805
|
#
|
|
961
|
-
#
|
|
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
|
-
#
|
|
964
|
-
#
|
|
965
|
-
#
|
|
966
|
-
# a
|
|
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
|
-
#
|
|
973
|
-
#
|
|
974
|
-
#
|
|
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
|
-
#
|
|
977
|
-
# a
|
|
978
|
-
# a.first
|
|
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
|
-
|
|
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
|
-
#
|
|
987
|
-
#
|
|
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
|
-
#
|
|
1847
|
+
# If `n >= array.size`, returns all elements:
|
|
1848
|
+
# a = [:foo, 'bar', 2]
|
|
1849
|
+
# a.first(50) # => [:foo, "bar", 2]
|
|
990
1850
|
#
|
|
991
|
-
#
|
|
992
|
-
#
|
|
993
|
-
# a
|
|
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
|
-
|
|
999
|
-
|
|
1000
|
-
# Flattens `self` in place.
|
|
1855
|
+
# Related: #last.
|
|
1001
1856
|
#
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
#
|
|
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
|
-
|
|
1008
|
-
|
|
1009
|
-
#
|
|
1010
|
-
#
|
|
1011
|
-
#
|
|
1012
|
-
#
|
|
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
|
-
#
|
|
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
|
|
1020
|
-
#
|
|
1021
|
-
#
|
|
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
|
-
#
|
|
1026
|
-
#
|
|
1027
|
-
#
|
|
1028
|
-
#
|
|
1029
|
-
#
|
|
1030
|
-
#
|
|
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
|
-
#
|
|
1035
|
-
#
|
|
1949
|
+
# <!-- rdoc-file=array.c -->
|
|
1950
|
+
# Returns the index of a specified element.
|
|
1036
1951
|
#
|
|
1037
|
-
#
|
|
1038
|
-
#
|
|
1039
|
-
#
|
|
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
|
-
#
|
|
1957
|
+
# Returns `nil` if no such element found.
|
|
1042
1958
|
#
|
|
1043
|
-
#
|
|
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
|
-
#
|
|
1046
|
-
#
|
|
1047
|
-
#
|
|
1048
|
-
# a
|
|
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
|
-
#
|
|
1053
|
-
#
|
|
1054
|
-
#
|
|
1055
|
-
#
|
|
1056
|
-
#
|
|
1057
|
-
#
|
|
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
|
-
#
|
|
1060
|
-
#
|
|
1061
|
-
# a
|
|
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
|
-
#
|
|
1066
|
-
#
|
|
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
|
-
#
|
|
2020
|
+
# Array#to_s is an alias for Array#inspect.
|
|
1069
2021
|
#
|
|
1070
2022
|
def inspect: () -> String
|
|
1071
2023
|
|
|
1072
|
-
#
|
|
1073
|
-
#
|
|
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
|
-
#
|
|
2049
|
+
# Preserves order from `self`:
|
|
2050
|
+
# [0, 1, 2].intersection([2, 1, 0]) # => [0, 1, 2]
|
|
1076
2051
|
#
|
|
1077
|
-
#
|
|
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
|
-
#
|
|
2054
|
+
# Related: Array#&.
|
|
1082
2055
|
#
|
|
1083
2056
|
def intersection: (*::Array[untyped] | _ToAry[untyped] other_ary) -> ::Array[Elem]
|
|
1084
2057
|
|
|
1085
|
-
#
|
|
1086
|
-
#
|
|
1087
|
-
#
|
|
1088
|
-
#
|
|
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
|
-
#
|
|
2070
|
+
# With no argument, joins using the output field separator, `$,`:
|
|
2071
|
+
# a = [:foo, 'bar', 2]
|
|
2072
|
+
# $, # => nil
|
|
2073
|
+
# a.join # => "foobar2"
|
|
1094
2074
|
#
|
|
1095
|
-
#
|
|
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
|
-
#
|
|
1100
|
-
#
|
|
1101
|
-
#
|
|
1102
|
-
#
|
|
1103
|
-
#
|
|
1104
|
-
#
|
|
1105
|
-
#
|
|
1106
|
-
# a
|
|
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
|
-
#
|
|
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
|
-
#
|
|
1114
|
-
#
|
|
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
|
-
#
|
|
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
|
-
#
|
|
1119
|
-
# a
|
|
1120
|
-
# a.last(
|
|
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
|
-
#
|
|
1126
|
-
#
|
|
1127
|
-
#
|
|
1128
|
-
#
|
|
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
|
-
#
|
|
1133
|
-
#
|
|
1134
|
-
#
|
|
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
|
-
#
|
|
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
|
-
#
|
|
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
|
-
#
|
|
1148
|
-
#
|
|
1149
|
-
#
|
|
1150
|
-
#
|
|
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
|
-
#
|
|
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
|
-
#
|
|
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
|
-
#
|
|
1163
|
-
#
|
|
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
|
-
#
|
|
1166
|
-
#
|
|
1167
|
-
#
|
|
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
|
-
#
|
|
2197
|
+
# When a block is given, the block must return an Integer.
|
|
1170
2198
|
#
|
|
1171
|
-
#
|
|
1172
|
-
#
|
|
1173
|
-
#
|
|
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
|
-
#
|
|
1181
|
-
#
|
|
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
|
-
#
|
|
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
|
-
#
|
|
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
|
-
#
|
|
1190
|
-
#
|
|
1191
|
-
#
|
|
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
|
-
#
|
|
1196
|
-
#
|
|
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
|
-
#
|
|
1199
|
-
#
|
|
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
|
-
#
|
|
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
|
-
#
|
|
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
|
-
#
|
|
1337
|
-
#
|
|
1338
|
-
#
|
|
1339
|
-
#
|
|
1340
|
-
#
|
|
1341
|
-
#
|
|
1342
|
-
#
|
|
1343
|
-
#
|
|
1344
|
-
#
|
|
1345
|
-
#
|
|
1346
|
-
#
|
|
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
|
-
#
|
|
1349
|
-
# a
|
|
1350
|
-
# a.permutation(
|
|
1351
|
-
#
|
|
1352
|
-
#
|
|
1353
|
-
#
|
|
1354
|
-
#
|
|
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
|
-
#
|
|
1360
|
-
#
|
|
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
|
-
#
|
|
1363
|
-
#
|
|
1364
|
-
#
|
|
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
|
-
#
|
|
1367
|
-
# a
|
|
1368
|
-
# a.pop(
|
|
1369
|
-
#
|
|
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
|
-
|
|
1375
|
-
|
|
1376
|
-
#
|
|
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
|
-
#
|
|
1382
|
-
# instead.
|
|
2581
|
+
# Array#prepend is an alias for Array#unshift.
|
|
1383
2582
|
#
|
|
1384
|
-
#
|
|
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
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
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
|
-
#
|
|
1397
|
-
#
|
|
1398
|
-
#
|
|
2643
|
+
# <!--
|
|
2644
|
+
# rdoc-file=array.c
|
|
2645
|
+
# - array.push(*objects) -> self
|
|
2646
|
+
# -->
|
|
2647
|
+
# Appends trailing elements.
|
|
1399
2648
|
#
|
|
1400
|
-
#
|
|
1401
|
-
# a
|
|
1402
|
-
#
|
|
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
|
-
|
|
1407
|
-
|
|
1408
|
-
#
|
|
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
|
-
#
|
|
2660
|
+
# Related: #pop, #shift, #unshift.
|
|
1411
2661
|
#
|
|
1412
|
-
|
|
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
|
-
#
|
|
2673
|
+
# Returns `nil` if no such element is found.
|
|
1415
2674
|
#
|
|
1416
|
-
#
|
|
1417
|
-
# a.rassoc("two") #=> [2, "two"]
|
|
1418
|
-
# a.rassoc("four") #=> nil
|
|
2675
|
+
# Related: #assoc.
|
|
1419
2676
|
#
|
|
1420
2677
|
alias rassoc assoc
|
|
1421
2678
|
|
|
1422
|
-
#
|
|
1423
|
-
#
|
|
1424
|
-
#
|
|
1425
|
-
#
|
|
1426
|
-
#
|
|
1427
|
-
#
|
|
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
|
-
#
|
|
1432
|
-
#
|
|
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
|
-
#
|
|
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
|
-
#
|
|
2707
|
+
# Returns `nil` if no elements removed.
|
|
1437
2708
|
#
|
|
1438
|
-
#
|
|
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
|
-
#
|
|
1444
|
-
#
|
|
1445
|
-
#
|
|
1446
|
-
#
|
|
1447
|
-
#
|
|
1448
|
-
#
|
|
1449
|
-
#
|
|
1450
|
-
#
|
|
1451
|
-
#
|
|
1452
|
-
#
|
|
1453
|
-
#
|
|
1454
|
-
#
|
|
1455
|
-
#
|
|
1456
|
-
#
|
|
1457
|
-
#
|
|
1458
|
-
# a.repeated_combination(
|
|
1459
|
-
#
|
|
1460
|
-
#
|
|
1461
|
-
#
|
|
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
|
-
#
|
|
1467
|
-
#
|
|
1468
|
-
#
|
|
1469
|
-
#
|
|
1470
|
-
#
|
|
1471
|
-
#
|
|
1472
|
-
#
|
|
1473
|
-
#
|
|
1474
|
-
#
|
|
1475
|
-
#
|
|
1476
|
-
#
|
|
1477
|
-
#
|
|
1478
|
-
#
|
|
1479
|
-
#
|
|
1480
|
-
#
|
|
1481
|
-
# a.repeated_permutation(
|
|
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
|
-
#
|
|
1487
|
-
#
|
|
1488
|
-
#
|
|
1489
|
-
# a = [
|
|
1490
|
-
# a.replace([
|
|
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
|
-
#
|
|
1496
|
-
#
|
|
1497
|
-
#
|
|
1498
|
-
#
|
|
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
|
-
#
|
|
1503
|
-
#
|
|
1504
|
-
#
|
|
1505
|
-
#
|
|
1506
|
-
#
|
|
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
|
-
#
|
|
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
|
-
#
|
|
1513
|
-
#
|
|
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
|
-
#
|
|
2883
|
+
# Output:
|
|
2884
|
+
# 2
|
|
2885
|
+
# bar
|
|
1516
2886
|
#
|
|
1517
|
-
#
|
|
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
|
-
#
|
|
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
|
-
#
|
|
1525
|
-
#
|
|
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
|
|
2924
|
+
# Returns `nil` if the block never returns a truthy value.
|
|
1528
2925
|
#
|
|
1529
|
-
#
|
|
2926
|
+
# When neither an argument nor a block is given, returns a new Enumerator:
|
|
1530
2927
|
#
|
|
1531
|
-
#
|
|
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
|
-
#
|
|
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
|
-
#
|
|
1543
|
-
#
|
|
1544
|
-
#
|
|
1545
|
-
#
|
|
1546
|
-
#
|
|
1547
|
-
#
|
|
1548
|
-
#
|
|
1549
|
-
#
|
|
1550
|
-
#
|
|
1551
|
-
#
|
|
1552
|
-
# 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
|
-
#
|
|
1557
|
-
#
|
|
1558
|
-
#
|
|
1559
|
-
#
|
|
1560
|
-
#
|
|
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
|
-
#
|
|
1563
|
-
# a
|
|
1564
|
-
# a
|
|
1565
|
-
#
|
|
1566
|
-
#
|
|
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
|
-
#
|
|
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
|
-
#
|
|
1573
|
-
#
|
|
1574
|
-
#
|
|
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
|
|
1577
|
-
# an empty array.
|
|
3035
|
+
# If `self` is empty, returns `nil`.
|
|
1578
3036
|
#
|
|
1579
|
-
#
|
|
1580
|
-
#
|
|
1581
|
-
# a.sample(
|
|
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
|
-
#
|
|
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
|
-
#
|
|
1592
|
-
#
|
|
1593
|
-
#
|
|
1594
|
-
#
|
|
1595
|
-
#
|
|
1596
|
-
#
|
|
1597
|
-
#
|
|
1598
|
-
#
|
|
1599
|
-
# a
|
|
1600
|
-
#
|
|
1601
|
-
#
|
|
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
|
-
#
|
|
1609
|
-
#
|
|
1610
|
-
#
|
|
1611
|
-
#
|
|
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
|
-
#
|
|
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
|
-
#
|
|
3095
|
+
# Returns `nil` if no elements were removed.
|
|
1616
3096
|
#
|
|
1617
|
-
#
|
|
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
|
-
#
|
|
1625
|
-
#
|
|
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
|
|
1628
|
-
#
|
|
1629
|
-
#
|
|
1630
|
-
#
|
|
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
|
-
#
|
|
1633
|
-
# args.shift #=> "-m"
|
|
1634
|
-
# args #=> ["-q", "filename"]
|
|
3131
|
+
# If `n` is zero, returns a new empty Array; `self` is unmodified.
|
|
1635
3132
|
#
|
|
1636
|
-
#
|
|
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
|
-
#
|
|
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
|
-
#
|
|
1656
|
-
#
|
|
1657
|
-
#
|
|
1658
|
-
#
|
|
1659
|
-
#
|
|
1660
|
-
#
|
|
1661
|
-
#
|
|
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
|
-
#
|
|
1670
|
-
#
|
|
1671
|
-
#
|
|
1672
|
-
#
|
|
1673
|
-
#
|
|
1674
|
-
#
|
|
1675
|
-
#
|
|
1676
|
-
#
|
|
1677
|
-
#
|
|
1678
|
-
#
|
|
1679
|
-
#
|
|
1680
|
-
# a = [
|
|
1681
|
-
# a[
|
|
1682
|
-
# a[
|
|
1683
|
-
#
|
|
1684
|
-
#
|
|
1685
|
-
#
|
|
1686
|
-
#
|
|
1687
|
-
#
|
|
1688
|
-
#
|
|
1689
|
-
# a[
|
|
1690
|
-
# a[
|
|
1691
|
-
#
|
|
1692
|
-
#
|
|
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
|
-
#
|
|
1699
|
-
#
|
|
1700
|
-
#
|
|
1701
|
-
#
|
|
1702
|
-
# range
|
|
1703
|
-
#
|
|
1704
|
-
#
|
|
1705
|
-
#
|
|
1706
|
-
#
|
|
1707
|
-
#
|
|
1708
|
-
# a
|
|
1709
|
-
# a.slice!(
|
|
1710
|
-
# 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
|
-
#
|
|
1717
|
-
#
|
|
1718
|
-
#
|
|
1719
|
-
#
|
|
1720
|
-
#
|
|
1721
|
-
#
|
|
1722
|
-
#
|
|
1723
|
-
#
|
|
1724
|
-
#
|
|
1725
|
-
#
|
|
1726
|
-
#
|
|
1727
|
-
#
|
|
1728
|
-
#
|
|
1729
|
-
#
|
|
1730
|
-
#
|
|
1731
|
-
#
|
|
1732
|
-
#
|
|
1733
|
-
#
|
|
1734
|
-
#
|
|
1735
|
-
#
|
|
1736
|
-
#
|
|
1737
|
-
#
|
|
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
|
-
#
|
|
1743
|
-
#
|
|
1744
|
-
#
|
|
1745
|
-
#
|
|
1746
|
-
#
|
|
1747
|
-
#
|
|
1748
|
-
#
|
|
1749
|
-
#
|
|
1750
|
-
#
|
|
1751
|
-
#
|
|
1752
|
-
#
|
|
1753
|
-
#
|
|
1754
|
-
#
|
|
1755
|
-
#
|
|
1756
|
-
#
|
|
1757
|
-
#
|
|
1758
|
-
#
|
|
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
|
-
#
|
|
1764
|
-
#
|
|
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
|
-
#
|
|
1767
|
-
#
|
|
3408
|
+
# For duplicates returned by the block, the ordering is indeterminate, and may
|
|
3409
|
+
# be unstable.
|
|
1768
3410
|
#
|
|
1769
|
-
#
|
|
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
|
-
#
|
|
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
|
-
#
|
|
1777
|
-
#
|
|
1778
|
-
#
|
|
1779
|
-
#
|
|
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
|
-
#
|
|
3434
|
+
# For example, `[e1, e2, e3].sum` returns `init + e1 + e2 + e3`.
|
|
1782
3435
|
#
|
|
1783
|
-
#
|
|
1784
|
-
# [
|
|
1785
|
-
#
|
|
1786
|
-
#
|
|
1787
|
-
#
|
|
1788
|
-
#
|
|
1789
|
-
#
|
|
1790
|
-
#
|
|
1791
|
-
#
|
|
1792
|
-
#
|
|
1793
|
-
#
|
|
1794
|
-
#
|
|
1795
|
-
#
|
|
1796
|
-
#
|
|
1797
|
-
#
|
|
1798
|
-
#
|
|
1799
|
-
#
|
|
1800
|
-
#
|
|
1801
|
-
#
|
|
1802
|
-
#
|
|
1803
|
-
#
|
|
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
|
-
#
|
|
1812
|
-
#
|
|
1813
|
-
#
|
|
1814
|
-
#
|
|
1815
|
-
#
|
|
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
|
-
#
|
|
1818
|
-
# a
|
|
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
|
-
#
|
|
1823
|
-
#
|
|
1824
|
-
#
|
|
1825
|
-
#
|
|
1826
|
-
#
|
|
1827
|
-
#
|
|
1828
|
-
#
|
|
1829
|
-
#
|
|
1830
|
-
#
|
|
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
|
-
#
|
|
1836
|
-
#
|
|
1837
|
-
#
|
|
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
|
-
#
|
|
1846
|
-
#
|
|
1847
|
-
#
|
|
1848
|
-
#
|
|
1849
|
-
#
|
|
1850
|
-
#
|
|
1851
|
-
#
|
|
1852
|
-
#
|
|
1853
|
-
#
|
|
1854
|
-
#
|
|
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
|
-
|
|
1860
|
-
|
|
1861
|
-
#
|
|
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
|
-
#
|
|
1864
|
-
# a.transpose #=> [[1, 3, 5], [2, 4, 6]]
|
|
3556
|
+
# Array#to_s is an alias for Array#inspect.
|
|
1865
3557
|
#
|
|
1866
|
-
|
|
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
|
-
#
|
|
1871
|
-
#
|
|
1872
|
-
#
|
|
1873
|
-
#
|
|
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
|
-
#
|
|
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
|
-
#
|
|
3584
|
+
# Related: Array#|.
|
|
1880
3585
|
#
|
|
1881
3586
|
def union: [T] (*::Array[T] other_arys) -> ::Array[T | Elem]
|
|
1882
3587
|
|
|
1883
|
-
#
|
|
1884
|
-
#
|
|
1885
|
-
#
|
|
1886
|
-
#
|
|
1887
|
-
#
|
|
1888
|
-
#
|
|
1889
|
-
#
|
|
1890
|
-
#
|
|
1891
|
-
#
|
|
1892
|
-
#
|
|
1893
|
-
#
|
|
1894
|
-
#
|
|
1895
|
-
#
|
|
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
|
-
#
|
|
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
|
-
#
|
|
3618
|
+
# With no block given, identifies and removes elements using method `eql?` to
|
|
3619
|
+
# compare.
|
|
1903
3620
|
#
|
|
1904
|
-
#
|
|
3621
|
+
# Returns `self` if any elements removed:
|
|
3622
|
+
# a = [0, 0, 1, 1, 2, 2]
|
|
3623
|
+
# a.uniq! # => [0, 1, 2]
|
|
1905
3624
|
#
|
|
1906
|
-
# `
|
|
3625
|
+
# Returns `nil` if no elements removed.
|
|
1907
3626
|
#
|
|
1908
|
-
#
|
|
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
|
-
#
|
|
1911
|
-
# a
|
|
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
|
-
#
|
|
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
|
-
#
|
|
1923
|
-
#
|
|
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
|
-
#
|
|
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
|
-
#
|
|
1932
|
-
#
|
|
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
|
-
#
|
|
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
|
-
#
|
|
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
|
-
#
|
|
1939
|
-
# a
|
|
1940
|
-
# a.values_at(
|
|
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
|
-
|
|
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
|
-
#
|
|
1950
|
-
#
|
|
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
|
-
#
|
|
1953
|
-
#
|
|
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
|
-
#
|
|
1956
|
-
#
|
|
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
|
-
|
|
1959
|
-
|
|
1960
|
-
#
|
|
1961
|
-
#
|
|
1962
|
-
#
|
|
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
|
-
#
|
|
1970
|
-
#
|
|
1971
|
-
#
|
|
1972
|
-
#
|
|
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
|
-
#
|
|
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
|
-
#
|
|
1984
|
-
#
|
|
1985
|
-
#
|
|
1986
|
-
#
|
|
1987
|
-
#
|
|
1988
|
-
#
|
|
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
|