Class: Array
Overview
Arrays are ordered, integer-indexed collections of any object. Array indexing starts at 0, as in C or Java. A negative index is assumed to be relative to the end of the array—that is, an index of -1 indicates the last element of the array, -2 is the next to last element in the array, and so on.
Class Method Summary collapse
-
.[] ⇒ Object
Returns a new array populated with the given objects.
Instance Method Summary collapse
-
#&(other_array) ⇒ Object
Set Intersection—Returns a new array containing elements common to the two arrays, with no duplicates.
-
#* ⇒ Object
Repetition—With a String argument, equivalent to self.join(str).
-
#+(other_array) ⇒ Array
Concatenation—Returns a new array built by concatenating the two arrays together to produce a third array.
-
#-(other_array) ⇒ Array
Array Difference—Returns a new array that is a copy of the original array, removing any items that also appear in other_array.
-
#<<(obj) ⇒ Array
Append—Pushes the given object on to the end of this array.
-
#<=>(other_array) ⇒ -1, ...
Comparison—Returns an integer (-1, 0, or +1) if this array is less than, equal to, or greater than other_array.
-
#==(other_array) ⇒ Boolean
Equality—Two arrays are equal if they contain the same number of elements and if each element is equal to (according to Object.==) the corresponding element in the other array.
-
#[] ⇒ Object
Element Reference—Returns the element at index, or returns a subarray starting at start and continuing for length elements, or returns a subarray specified by range.
-
#[]= ⇒ Object
Element Assignment—Sets the element at index, or replaces a subarray starting at start and continuing for length elements, or replaces a subarray specified by range.
-
#assoc(obj) ⇒ Array?
Searches through an array whose elements are also arrays comparing obj with the first element of each contained array using obj.==.
-
#at(index) ⇒ Object?
Returns the element at index.
-
#clear ⇒ Array
Removes all elements from self.
-
#collect ⇒ Object
Invokes block once for each element of self.
-
#collect! ⇒ Object
Invokes the block once for each element of self, replacing the element with the value returned by block.
-
#compact ⇒ Array
Returns a copy of self with all
nil
elements removed. -
#compact! ⇒ Array?
Removes
nil
elements from array. -
#concat(other_array) ⇒ Array
Appends the elements in other_array to self.
-
#delete ⇒ Object
Deletes items from self that are equal to obj.
-
#delete_at(index) ⇒ Object?
Deletes the element at the specified index, returning that element, or
nil
if the index is out of range. -
#delete_if {|item| ... } ⇒ Array
Deletes every element of self for which block evaluates to
true
. -
#each {|item| ... } ⇒ Array
Calls block once for each element in self, passing that element as a parameter.
-
#each_index {|index| ... } ⇒ Array
Same as
Array#each
, but passes the index of the element instead of the element itself. -
#empty? ⇒ Boolean
Returns
true
if self array contains no elements. -
#eql?(other) ⇒ Boolean
Returns
true
if array and other are the same object, or are both arrays with the same content. -
#fetch ⇒ Object
Tries to return the element at position index.
-
#fill ⇒ Object
The first three forms set the selected elements of self (which may be the entire array) to obj.
-
#first ⇒ Object
Returns the first element, or the first
n
elements, of the array. -
#flatten ⇒ Array
Returns a new array that is a one-dimensional flattening of this array (recursively).
-
#flatten! ⇒ Array?
Flattens self in place.
-
#frozen? ⇒ Boolean
Return
true
if this array is frozen (or temporarily frozen while being sorted). -
#hash ⇒ Fixnum
Compute a hash-code for this array.
-
#include?(obj) ⇒ Boolean
Returns
true
if the given object is present in self (that is, if any object==
anObject),false
otherwise. -
#index(obj) ⇒ Integer?
Returns the index of the first object in self such that is
==
to obj. -
#indexes ⇒ Object
Deprecated; use
Array#values_at
. -
#indices ⇒ Object
Deprecated; use
Array#values_at
. -
#initialize ⇒ Object
constructor
Returns a new array.
-
#replace(other_array) ⇒ Array
Replaces the contents of self with the contents of other_array, truncating or expanding if necessary.
-
#insert(index, obj...) ⇒ Array
Inserts the given values before the element with the given index (which may be negative).
-
#inspect ⇒ String
Create a printable version of array.
-
#join(sep = $,) ⇒ String
Returns a string created by converting each element of the array to a string, separated by sep.
-
#last ⇒ Object
Returns the last element(s) of self.
-
#length ⇒ Integer
(also: #size)
Returns the number of elements in self.
-
#map ⇒ Object
Invokes block once for each element of self.
-
#map! ⇒ Object
Invokes the block once for each element of self, replacing the element with the value returned by block.
-
#nitems ⇒ Integer
Returns the number of non-
nil
elements in self. -
#pack ⇒ Object
Packs the contents of arr into a binary sequence according to the directives in aTemplateString (see the table below) Directives “A,” “a,” and “Z” may be followed by a count, which gives the width of the resulting field.
-
#pop ⇒ Object?
Removes the last element from self and returns it, or
nil
if the array is empty. -
#push(obj, ...) ⇒ Array
Append—Pushes the given object(s) on to the end of this array.
-
#rassoc(key) ⇒ Array?
Searches through the array whose elements are also arrays.
-
#reject {|item| ... } ⇒ Array
Returns a new array containing the items in self for which the block is not true.
-
#reject! {|item| ... } ⇒ Array?
Equivalent to
Array#delete_if
, deleting elements from self for which the block evaluates to true, but returnsnil
if no changes were made. -
#replace(other_array) ⇒ Array
Replaces the contents of self with the contents of other_array, truncating or expanding if necessary.
-
#reverse ⇒ Array
Returns a new array containing self’s elements in reverse order.
-
#reverse! ⇒ Array
Reverses self in place.
-
#reverse_each {|item| ... } ⇒ Object
Same as
Array#each
, but traverses self in reverse order. -
#rindex(obj) ⇒ Integer?
Returns the index of the last object in array
==
to obj. -
#select {|item| ... } ⇒ Array
Invokes the block passing in successive elements from array, returning an array containing those elements for which the block returns a true value (equivalent to
Enumerable#select
). -
#shift ⇒ Object?
Returns the first element of self and removes it (shifting all other elements down by one).
-
#slice ⇒ Object
Element Reference—Returns the element at index, or returns a subarray starting at start and continuing for length elements, or returns a subarray specified by range.
-
#slice! ⇒ Object
Deletes the element(s) given by an index (optionally with a length) or by a range.
-
#sort ⇒ Object
Returns a new array created by sorting self.
-
#sort! ⇒ Object
Sorts self.
-
#to_a ⇒ Array
Returns self.
-
#to_ary ⇒ Array
Returns self.
-
#to_s ⇒ String
Returns self
.join
. -
#transpose ⇒ Array
Assumes that self is an array of arrays and transposes the rows and columns.
-
#uniq ⇒ Array
Returns a new array by removing duplicate values in self.
-
#uniq! ⇒ Array?
Removes duplicate elements from self.
-
#unshift(obj, ...) ⇒ Array
Prepends objects to the front of array.
-
#values_at(selector, ...) ⇒ Array
Returns an array containing the elements in self corresponding to the given selector(s).
-
#zip ⇒ Object
Converts any arguments to arrays, then merges elements of self with corresponding elements from each argument.
-
#|(other_array) ⇒ Array
Set Union—Returns a new array by joining this array with other_array, removing duplicates.
Methods included from Enumerable
#all?, #any?, #detect, #each_with_index, #entries, #find, #find_all, #grep, #inject, #max, #member?, #min, #partition, #sort_by
Constructor Details
#new(size = 0, obj = nil) ⇒ Object #new(array) ⇒ Object #new(size) {|index| ... } ⇒ Object
Returns a new array. In the first form, the new array is empty. In the second it is created with size copies of obj (that is, size references to the same obj). The third form creates a copy of the array passed as a parameter (the array is generated by calling to_ary on the parameter). In the last form, an array of the given size is created. Each element in this array is calculated by passing the element’s index to the given block and storing the return value.
Array.new
Array.new(2)
Array.new(5, "A")
# only one copy of the object is created
a = Array.new(2, Hash.new)
a[0]['cat'] = 'feline'
a
a[1]['cat'] = 'Felix'
a
# here multiple copies are created
a = Array.new(2) { Hash.new }
a[0]['cat'] = 'feline'
a
squares = Array.new(5) {|i| i*i}
squares
copy = Array.new(squares)
266 267 268 |
# File 'array.c', line 266 static VALUE rb_ary_initialize(argc, argv, ary) int argc; |
Class Method Details
Instance Method Details
#&(other_array) ⇒ Object
Set Intersection—Returns a new array containing elements common to the two arrays, with no duplicates.
[ 1, 1, 3, 5 ] & [ 1, 2, 3 ] #=> [ 1, 3 ]
2676 2677 2678 |
# File 'array.c', line 2676 static VALUE rb_ary_and(ary1, ary2) VALUE ary1, ary2; |
#*(int) ⇒ Array #*(str) ⇒ String
Repetition—With a String argument, equivalent to self.join(str). Otherwise, returns a new array built by concatenating the int copies of self.
[ 1, 2, 3 ] * 3 #=> [ 1, 2, 3, 1, 2, 3, 1, 2, 3 ]
[ 1, 2, 3 ] * "," #=> "1,2,3"
2361 2362 2363 |
# File 'array.c', line 2361 static VALUE rb_ary_times(ary, times) VALUE ary, times; |
#+(other_array) ⇒ Array
Concatenation—Returns a new array built by concatenating the two arrays together to produce a third array.
[ 1, 2, 3 ] + [ 4, 5 ] #=> [ 1, 2, 3, 4, 5 ]
2308 2309 2310 |
# File 'array.c', line 2308 VALUE rb_ary_plus(x, y) VALUE x, y; |
#-(other_array) ⇒ Array
Array Difference—Returns a new array that is a copy of the original array, removing any items that also appear in other_array. (If you need set-like behavior, see the library class Set.)
[ 1, 1, 2, 2, 3, 3, 4, 5 ] - [ 1, 2, 4 ] #=> [ 3, 3, 5 ]
2647 2648 2649 |
# File 'array.c', line 2647 static VALUE rb_ary_diff(ary1, ary2) VALUE ary1, ary2; |
#<<(obj) ⇒ Array
Append—Pushes the given object on to the end of this array. This expression returns the array itself, so several appends may be chained together.
[ 1, 2 ] << "c" << "d" << [ 3, 4 ]
#=> [ 1, 2, "c", "d", [ 3, 4 ] ]
400 401 402 |
# File 'array.c', line 400 VALUE rb_ary_push(ary, item) VALUE ary; |
#<=>(other_array) ⇒ -1, ...
Comparison—Returns an integer (-1, 0, or +1) if this array is less than, equal to, or greater than other_array. Each object in each array is compared (using <=>). If any value isn’t equal, then that inequality is the return value. If all the values found are equal, then the return is based on a comparison of the array lengths. Thus, two arrays are “equal” according to Array#<=>
if and only if they have the same length and the value of each element is equal to the value of the corresponding element in the other array.
[ "a", "a", "c" ] <=> [ "a", "b", "c" ] #=> -1
[ 1, 2, 3, 4, 5, 6 ] <=> [ 1, 2 ] #=> +1
2594 2595 2596 |
# File 'array.c', line 2594 VALUE rb_ary_cmp(ary1, ary2) VALUE ary1, ary2; |
#==(other_array) ⇒ Boolean
Equality—Two arrays are equal if they contain the same number of elements and if each element is equal to (according to Object.==) the corresponding element in the other array.
[ "a", "c" ] == [ "a", "c", 7 ] #=> false
[ "a", "c", 7 ] == [ "a", "c", 7 ] #=> true
[ "a", "c", 7 ] == [ "a", "d", "f" ] #=> false
2476 2477 2478 |
# File 'array.c', line 2476 static VALUE rb_ary_equal(ary1, ary2) VALUE ary1, ary2; |
#[](index) ⇒ Object? #[](start, length) ⇒ Array? #[](range) ⇒ Array? #slice(index) ⇒ Object? #slice(start, length) ⇒ Array? #slice(range) ⇒ Array?
Element Reference—Returns the element at index, or returns a subarray starting at start and continuing for length elements, or returns a subarray specified by range. Negative indices count backward from the end of the array (-1 is the last element). Returns nil if the index (or starting index) are out of range.
a = [ "a", "b", "c", "d", "e" ]
a[2] + a[0] + a[1] #=> "cab"
a[6] #=> nil
a[1, 2] #=> [ "b", "c" ]
a[1..3] #=> [ "b", "c", "d" ]
a[4..7] #=> [ "e" ]
a[6..10] #=> nil
a[-3, 3] #=> [ "c", "d", "e" ]
# special cases
a[5] #=> nil
a[5, 1] #=> []
a[5..10] #=> []
660 661 662 |
# File 'array.c', line 660 VALUE rb_ary_aref(argc, argv, ary) int argc; |
#[]=(index) ⇒ Object #[]=(start, length) ⇒ Object, ... #[]=(range) ⇒ Object, ...
Element Assignment—Sets the element at index, or replaces a subarray starting at start and continuing for length elements, or replaces a subarray specified by range. If indices are greater than the current capacity of the array, the array grows automatically. A negative indices will count backward from the end of the array. Inserts elements if length is zero. If nil
is used in the second and third form, deletes elements from self. An IndexError
is raised if a negative index points past the beginning of the array. See also Array#push
, and Array#unshift
.
a = Array.new
a[4] = "4"; #=> [nil, nil, nil, nil, "4"]
a[0, 3] = [ 'a', 'b', 'c' ] #=> ["a", "b", "c", nil, "4"]
a[1..2] = [ 1, 2 ] #=> ["a", 1, 2, nil, "4"]
a[0, 2] = "?" #=> ["?", 2, nil, "4"]
a[0..2] = "A" #=> ["A", "4"]
a[-1] = "Z" #=> ["A", "Z"]
a[1..-1] = nil #=> ["A"]
1041 1042 1043 |
# File 'array.c', line 1041 static VALUE rb_ary_aset(argc, argv, ary) int argc; |
#assoc(obj) ⇒ Array?
Searches through an array whose elements are also arrays comparing obj with the first element of each contained array using obj.==. Returns the first contained array that matches (that is, the first associated array), or nil
if no match is found. See also Array#rassoc
.
s1 = [ "colors", "red", "blue", "green" ]
s2 = [ "letters", "a", "b", "c" ]
s3 = "foo"
a = [ s1, s2, s3 ]
a.assoc("letters") #=> [ "letters", "a", "b", "c" ]
a.assoc("foo") #=> nil
2414 2415 2416 |
# File 'array.c', line 2414 VALUE rb_ary_assoc(ary, key) VALUE ary, key; |
#at(index) ⇒ Object?
Returns the element at index. A negative index counts from the end of self. Returns nil
if the index is out of range. See also Array#[]
. (Array#at
is slightly faster than Array#[]
, as it does not accept ranges and so on.)
a = [ "a", "b", "c", "d", "e" ]
a.at(0) #=> "a"
a.at(-1) #=> "e"
718 719 720 |
# File 'array.c', line 718 static VALUE rb_ary_at(ary, pos) VALUE ary, pos; |
#clear ⇒ Array
Removes all elements from self.
a = [ "a", "b", "c", "d", "e" ]
a.clear #=> [ ]
2192 2193 2194 |
# File 'array.c', line 2192 VALUE rb_ary_clear(ary) VALUE ary; |
#collect {|item| ... } ⇒ Array #map {|item| ... } ⇒ Array
Invokes block once for each element of self. Creates a new array containing the values returned by the block. See also Enumerable#collect
.
a = [ "a", "b", "c", "d" ]
a.collect {|x| x + "!" } #=> ["a!", "b!", "c!", "d!"]
a #=> ["a", "b", "c", "d"]
1699 1700 1701 |
# File 'array.c', line 1699 static VALUE rb_ary_collect(ary) VALUE ary; |
#collect! {|item| ... } ⇒ Array #map! {|item| ... } ⇒ Array
Invokes the block once for each element of self, replacing the element with the value returned by block. See also Enumerable#collect
.
a = [ "a", "b", "c", "d" ]
a.collect! {|x| x + "!" }
a #=> [ "a!", "b!", "c!", "d!" ]
1731 1732 1733 |
# File 'array.c', line 1731 static VALUE rb_ary_collect_bang(ary) VALUE ary; |
#compact ⇒ Array
Returns a copy of self with all nil
elements removed.
[ "a", nil, "b", nil, "c", nil ].compact
#=> [ "a", "b", "c" ]
2836 2837 2838 |
# File 'array.c', line 2836 static VALUE rb_ary_compact(ary) VALUE ary; |
#compact! ⇒ Array?
Removes nil
elements from array. Returns nil
if no changes were made.
[ "a", nil, "b", nil, "c" ].compact! #=> [ "a", "b", "c" ]
[ "a", "b", "c" ].compact! #=> nil
2803 2804 2805 |
# File 'array.c', line 2803 static VALUE rb_ary_compact_bang(ary) VALUE ary; |
#concat(other_array) ⇒ Array
Appends the elements in other_array to self.
[ "a", "b" ].concat( ["c", "d"] ) #=> [ "a", "b", "c", "d" ]
2334 2335 2336 |
# File 'array.c', line 2334 VALUE rb_ary_concat(x, y) VALUE x, y; |
#delete(obj) ⇒ Object? #delete(obj) { ... } ⇒ Object?
Deletes items from self that are equal to obj. If the item is not found, returns nil
. If the optional code block is given, returns the result of block if the item is not found.
a = [ "a", "b", "b", "b", "c" ]
a.delete("b") #=> "b"
a #=> ["a", "c"]
a.delete("z") #=> nil
a.delete("z") { "not found" } #=> "not found"
1847 1848 1849 |
# File 'array.c', line 1847 VALUE rb_ary_delete(ary, item) VALUE ary; |
#delete_at(index) ⇒ Object?
Deletes the element at the specified index, returning that element, or nil
if the index is out of range. See also Array#slice!
.
a = %w( ant bat cat dog )
a.delete_at(2) #=> "cat"
a #=> ["ant", "bat", "dog"]
a.delete_at(99) #=> nil
1921 1922 1923 |
# File 'array.c', line 1921 static VALUE rb_ary_delete_at_m(ary, pos) VALUE ary, pos; |
#delete_if {|item| ... } ⇒ Array
Deletes every element of self for which block evaluates to true
.
a = [ "a", "b", "c" ]
a.delete_if {|x| x >= "b" } #=> ["a"]
2041 2042 2043 |
# File 'array.c', line 2041 static VALUE rb_ary_delete_if(ary) VALUE ary; |
#each {|item| ... } ⇒ Array
Calls block once for each element in self, passing that element as a parameter.
a = [ "a", "b", "c" ]
a.each {|x| print x, " -- " }
produces:
a -- b -- c --
1131 1132 1133 |
# File 'array.c', line 1131 VALUE rb_ary_each(ary) VALUE ary; |
#each_index {|index| ... } ⇒ Array
Same as Array#each
, but passes the index of the element instead of the element itself.
a = [ "a", "b", "c" ]
a.each_index {|x| print x, " -- " }
produces:
0 -- 1 -- 2 --
1158 1159 1160 |
# File 'array.c', line 1158 static VALUE rb_ary_each_index(ary) VALUE ary; |
#empty? ⇒ Boolean
Returns true
if self array contains no elements.
[].empty? #=> true
1225 1226 1227 |
# File 'array.c', line 1225 static VALUE rb_ary_empty_p(ary) VALUE ary; |
#eql?(other) ⇒ Boolean
Returns true
if array and other are the same object, or are both arrays with the same content.
2505 2506 2507 |
# File 'array.c', line 2505 static VALUE rb_ary_eql(ary1, ary2) VALUE ary1, ary2; |
#fetch(index) ⇒ Object #fetch(index, default) ⇒ Object #fetch(index) {|index| ... } ⇒ Object
Tries to return the element at position index. If the index lies outside the array, the first form throws an IndexError
exception, the second form returns default, and the third form returns the value of invoking the block, passing in the index. Negative values of index count from the end of the array.
a = [ 11, 22, 33, 44 ]
a.fetch(1) #=> 22
a.fetch(-1) #=> 44
a.fetch(4, 'cat') #=> "cat"
a.fetch(4) { |i| i*i } #=> 16
821 822 823 |
# File 'array.c', line 821 static VALUE rb_ary_fetch(argc, argv, ary) int argc; |
#fill(obj) ⇒ Array #fill(obj, start[, length]) ⇒ Array #fill(obj, range) ⇒ Array #fill {|index| ... } ⇒ Array #fill(start[, length]) {|index| ... } ⇒ Array #fill(range) {|index| ... } ⇒ Array
The first three forms set the selected elements of self (which may be the entire array) to obj. A start of nil
is equivalent to zero. A length of nil
is equivalent to self.length. The last three forms fill the array with the value of the block. The block is passed the absolute index of each element to be filled.
a = [ "a", "b", "c", "d" ]
a.fill("x") #=> ["x", "x", "x", "x"]
a.fill("z", 2, 2) #=> ["x", "x", "z", "z"]
a.fill("y", 0..1) #=> ["y", "y", "z", "z"]
a.fill {|i| i*i} #=> [0, 1, 4, 9]
a.fill(-2) {|i| i*i*i} #=> [0, 1, 8, 27]
2229 2230 2231 |
# File 'array.c', line 2229 static VALUE rb_ary_fill(argc, argv, ary) int argc; |
#first ⇒ Object? #first(n) ⇒ Array
Returns the first element, or the first n
elements, of the array. If the array is empty, the first form returns nil
, and the second form returns an empty array.
a = [ "q", "r", "s", "t" ]
a.first #=> "q"
a.first(1) #=> ["q"]
a.first(3) #=> ["q", "r", "s"]
740 741 742 |
# File 'array.c', line 740 static VALUE rb_ary_first(argc, argv, ary) int argc; |
#flatten ⇒ Array
Returns a new array that is a one-dimensional flattening of this array (recursively). That is, for every element that is an array, extract its elements into the new array.
s = [ 1, 2, 3 ] #=> [1, 2, 3]
t = [ 4, 5, 6, [7, 8] ] #=> [4, 5, 6, [7, 8]]
a = [ s, t, 9, 10 ] #=> [[1, 2, 3], [4, 5, 6, [7, 8]], 9, 10]
a.flatten #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10
2957 2958 2959 |
# File 'array.c', line 2957 static VALUE rb_ary_flatten(ary) VALUE ary; |
#flatten! ⇒ Array?
Flattens self in place. Returns nil
if no modifications were made (i.e., array contains no subarrays.)
a = [ 1, 2, [3, [4, 5] ] ]
a.flatten! #=> [1, 2, 3, 4, 5]
a.flatten! #=> nil
a #=> [1, 2, 3, 4, 5]
2917 2918 2919 |
# File 'array.c', line 2917 static VALUE rb_ary_flatten_bang(ary) VALUE ary; |
#frozen? ⇒ Boolean
Return true
if this array is frozen (or temporarily frozen while being sorted).
89 90 91 |
# File 'array.c', line 89 static VALUE rb_ary_frozen_p(ary) VALUE ary; |
#hash ⇒ Fixnum
Compute a hash-code for this array. Two arrays with the same content will have the same hash code (and will compare using eql?
).
2529 2530 2531 |
# File 'array.c', line 2529 static VALUE rb_ary_hash(ary) VALUE ary; |
#include?(obj) ⇒ Boolean
Returns true
if the given object is present in self (that is, if any object ==
anObject), false
otherwise.
a = [ "a", "b", "c" ]
a.include?("b") #=> true
a.include?("z") #=> false
2558 2559 2560 |
# File 'array.c', line 2558 VALUE rb_ary_includes(ary, item) VALUE ary; |
#index(obj) ⇒ Integer?
Returns the index of the first object in self such that is ==
to obj. Returns nil
if no match is found.
a = [ "a", "b", "c" ]
a.index("b") #=> 1
a.index("z") #=> nil
864 865 866 |
# File 'array.c', line 864 static VALUE rb_ary_index(ary, val) VALUE ary; |
#indexes(i1, i2, ...iN) ⇒ Array #indices(i1, i2, ...iN) ⇒ Array
Deprecated; use Array#values_at
.
917 918 919 |
# File 'array.c', line 917 static VALUE rb_ary_indexes(argc, argv, ary) int argc; |
#indexes(i1, i2, ...iN) ⇒ Array #indices(i1, i2, ...iN) ⇒ Array
Deprecated; use Array#values_at
.
917 918 919 |
# File 'array.c', line 917 static VALUE rb_ary_indexes(argc, argv, ary) int argc; |
#replace(other_array) ⇒ Array
Replaces the contents of self with the contents of other_array, truncating or expanding if necessary.
a = [ "a", "b", "c", "d", "e" ]
a.replace([ "x", "y", "z" ]) #=> ["x", "y", "z"]
a #=> ["x", "y", "z"]
2162 2163 2164 |
# File 'array.c', line 2162 static VALUE rb_ary_replace(copy, orig) VALUE copy, orig; |
#insert(index, obj...) ⇒ Array
Inserts the given values before the element with the given index (which may be negative).
a = %w{ a b c d }
a.insert(2, 99) #=> ["a", "b", 99, "c", "d"]
a.insert(-2, 1, 2, 3) #=> ["a", "b", 99, "c", 1, 2, 3, "d"]
1093 1094 1095 |
# File 'array.c', line 1093 static VALUE rb_ary_insert(argc, argv, ary) int argc; |
#inspect ⇒ String
Create a printable version of array.
1459 1460 1461 |
# File 'array.c', line 1459 static VALUE rb_ary_inspect(ary) VALUE ary; |
#join(sep = $,) ⇒ String
Returns a string created by converting each element of the array to a string, separated by sep.
[ "a", "b", "c" ].join #=> "abc"
[ "a", "b", "c" ].join("-") #=> "a-b-c"
1317 1318 1319 |
# File 'array.c', line 1317 static VALUE rb_ary_join_m(argc, argv, ary) int argc; |
#last ⇒ Object? #last(n) ⇒ Array
Returns the last element(s) of self. If the array is empty, the first form returns nil
.
[ "w", "x", "y", "z" ].last #=> "z"
776 777 778 |
# File 'array.c', line 776 static VALUE rb_ary_last(argc, argv, ary) int argc; |
#length ⇒ Integer Also known as: size
Returns the number of elements in self. May be zero.
[ 1, 2, 3, 4, 5 ].length #=> 5
1209 1210 1211 |
# File 'array.c', line 1209 static VALUE rb_ary_length(ary) VALUE ary; |
#collect {|item| ... } ⇒ Array #map {|item| ... } ⇒ Array
Invokes block once for each element of self. Creates a new array containing the values returned by the block. See also Enumerable#collect
.
a = [ "a", "b", "c", "d" ]
a.collect {|x| x + "!" } #=> ["a!", "b!", "c!", "d!"]
a #=> ["a", "b", "c", "d"]
1699 1700 1701 |
# File 'array.c', line 1699 static VALUE rb_ary_collect(ary) VALUE ary; |
#collect! {|item| ... } ⇒ Array #map! {|item| ... } ⇒ Array
Invokes the block once for each element of self, replacing the element with the value returned by block. See also Enumerable#collect
.
a = [ "a", "b", "c", "d" ]
a.collect! {|x| x + "!" }
a #=> [ "a!", "b!", "c!", "d!" ]
1731 1732 1733 |
# File 'array.c', line 1731 static VALUE rb_ary_collect_bang(ary) VALUE ary; |
#nitems ⇒ Integer
Returns the number of non-nil
elements in self. May be zero.
[ 1, nil, 3, nil, 5 ].nitems #=> 3
2855 2856 2857 |
# File 'array.c', line 2855 static VALUE rb_ary_nitems(ary) VALUE ary; |
#pack ⇒ Object
Packs the contents of arr into a binary sequence according to the directives in aTemplateString (see the table below) Directives “A,” “a,” and “Z” may be followed by a count, which gives the width of the resulting field. The remaining directives also may take a count, indicating the number of array elements to convert. If the count is an asterisk (“*
”), all remaining array elements will be converted. Any of the directives “sSiIlL
” may be followed by an underscore (“_
”) to use the underlying platform’s native size for the specified type; otherwise, they use a platform-independent size. Spaces are ignored in the template string. See also String#unpack
.
a = [ "a", "b", "c" ]
n = [ 65, 66, 67 ]
a.pack("A3A3A3") #=> "a b c "
a.pack("a3a3a3") #=> "a\000\000b\000\000c\000\000"
n.pack("ccc") #=> "ABC"
Directives for pack
.
Directive Meaning
---------------------------------------------------------------
@ | Moves to absolute position
A | ASCII string (space padded, count is width)
a | ASCII string (null padded, count is width)
B | Bit string (descending bit order)
b | Bit string (ascending bit order)
C | Unsigned char
c | Char
D, d | Double-precision float, native format
E | Double-precision float, little-endian byte order
e | Single-precision float, little-endian byte order
F, f | Single-precision float, native format
G | Double-precision float, network (big-endian) byte order
g | Single-precision float, network (big-endian) byte order
H | Hex string (high nibble first)
h | Hex string (low nibble first)
I | Unsigned integer
i | Integer
L | Unsigned long
l | Long
M | Quoted printable, MIME encoding (see RFC2045)
m | Base64 encoded string
N | Long, network (big-endian) byte order
n | Short, network (big-endian) byte-order
P | Pointer to a structure (fixed-length string)
p | Pointer to a null-terminated string
Q, q | 64-bit number
S | Unsigned short
s | Short
U | UTF-8
u | UU-encoded string
V | Long, little-endian byte order
v | Short, little-endian byte order
w | BER-compressed integer\fnm
X | Back up a byte
x | Null byte
Z | Same as ``a'', except that null is added with *
440 441 442 |
# File 'pack.c', line 440 static VALUE pack_pack(ary, fmt) VALUE ary, fmt; |
#pop ⇒ Object?
Removes the last element from self and returns it, or nil
if the array is empty.
a = [ "a", "m", "z" ]
a.pop #=> "z"
a #=> ["a", "m"]
446 447 448 |
# File 'array.c', line 446 VALUE rb_ary_pop(ary) VALUE ary; |
#push(obj, ...) ⇒ Array
Append—Pushes the given object(s) on to the end of this array. This expression returns the array itself, so several appends may be chained together.
a = [ "a", "b", "c" ]
a.push("d", "e", "f")
#=> ["a", "b", "c", "d", "e", "f"]
422 423 424 |
# File 'array.c', line 422 static VALUE rb_ary_push_m(argc, argv, ary) int argc; |
#rassoc(key) ⇒ Array?
Searches through the array whose elements are also arrays. Compares key with the second element of each contained array using ==
. Returns the first contained array that matches. See also Array#assoc
.
a = [ [ 1, "one"], [2, "two"], [3, "three"], ["ii", "two"] ]
a.rassoc("two") #=> [2, "two"]
a.rassoc("four") #=> nil
2445 2446 2447 |
# File 'array.c', line 2445 VALUE rb_ary_rassoc(ary, value) VALUE ary, value; |
#reject {|item| ... } ⇒ Array
Returns a new array containing the items in self for which the block is not true.
2021 2022 2023 |
# File 'array.c', line 2021 static VALUE rb_ary_reject(ary) VALUE ary; |
#reject! {|item| ... } ⇒ Array?
Equivalent to Array#delete_if
, deleting elements from self for which the block evaluates to true, but returns nil
if no changes were made. Also see Enumerable#reject
.
1991 1992 1993 |
# File 'array.c', line 1991 static VALUE rb_ary_reject_bang(ary) VALUE ary; |
#replace(other_array) ⇒ Array
Replaces the contents of self with the contents of other_array, truncating or expanding if necessary.
a = [ "a", "b", "c", "d", "e" ]
a.replace([ "x", "y", "z" ]) #=> ["x", "y", "z"]
a #=> ["x", "y", "z"]
2162 2163 2164 |
# File 'array.c', line 2162 static VALUE rb_ary_replace(copy, orig) VALUE copy, orig; |
#reverse ⇒ Array
Returns a new array containing self’s elements in reverse order.
[ "a", "b", "c" ].reverse #=> ["c", "b", "a"]
[ 1 ].reverse #=> [1]
1551 1552 1553 |
# File 'array.c', line 1551 static VALUE rb_ary_reverse_m(ary) VALUE ary; |
#reverse! ⇒ Array
Reverses self in place.
a = [ "a", "b", "c" ]
a.reverse! #=> ["c", "b", "a"]
a #=> ["c", "b", "a"]
1534 1535 1536 |
# File 'array.c', line 1534 static VALUE rb_ary_reverse_bang(ary) VALUE ary; |
#reverse_each {|item| ... } ⇒ Object
Same as Array#each
, but traverses self in reverse order.
a = [ "a", "b", "c" ]
a.reverse_each {|x| print x, " " }
produces:
c b a
1185 1186 1187 |
# File 'array.c', line 1185 static VALUE rb_ary_reverse_each(ary) VALUE ary; |
#rindex(obj) ⇒ Integer?
Returns the index of the last object in array ==
to obj. Returns nil
if no match is found.
a = [ "a", "b", "b", "b", "c" ]
a.rindex("b") #=> 3
a.rindex("z") #=> nil
891 892 893 |
# File 'array.c', line 891 static VALUE rb_ary_rindex(ary, val) VALUE ary; |
#select {|item| ... } ⇒ Array
Invokes the block passing in successive elements from array, returning an array containing those elements for which the block returns a true value (equivalent to Enumerable#select
).
a = %w{ a b c d e f }
a.select {|v| v =~ /[aeiou]/} #=> ["a", "e"]
1814 1815 1816 |
# File 'array.c', line 1814 static VALUE rb_ary_select(ary) VALUE ary; |
#shift ⇒ Object?
Returns the first element of self and removes it (shifting all other elements down by one). Returns nil
if the array is empty.
args = [ "-m", "-q", "filename" ]
args.shift #=> "-m"
args #=> ["-q", "filename"]
495 496 497 |
# File 'array.c', line 495 VALUE rb_ary_shift(ary) VALUE ary; |
#[](index) ⇒ Object? #[](start, length) ⇒ Array? #[](range) ⇒ Array? #slice(index) ⇒ Object? #slice(start, length) ⇒ Array? #slice(range) ⇒ Array?
Element Reference—Returns the element at index, or returns a subarray starting at start and continuing for length elements, or returns a subarray specified by range. Negative indices count backward from the end of the array (-1 is the last element). Returns nil if the index (or starting index) are out of range.
a = [ "a", "b", "c", "d", "e" ]
a[2] + a[0] + a[1] #=> "cab"
a[6] #=> nil
a[1, 2] #=> [ "b", "c" ]
a[1..3] #=> [ "b", "c", "d" ]
a[4..7] #=> [ "e" ]
a[6..10] #=> nil
a[-3, 3] #=> [ "c", "d", "e" ]
# special cases
a[5] #=> nil
a[5, 1] #=> []
a[5..10] #=> []
660 661 662 |
# File 'array.c', line 660 VALUE rb_ary_aref(argc, argv, ary) int argc; |
#slice!(index) ⇒ Object? #slice!(start, length) ⇒ nil #slice!(range) ⇒ nil
Deletes the element(s) given by an index (optionally with a length) or by a range. Returns the deleted object, subarray, or nil
if the index is out of range. Equivalent to:
def slice!(*args)
result = self[*args]
self[*args] = nil
result
end
a = [ "a", "b", "c" ]
a.slice!(1) #=> "b"
a #=> ["a", "c"]
a.slice!(-1) #=> "c"
a #=> ["a"]
a.slice!(100) #=> nil
a #=> ["a"]
1953 1954 1955 |
# File 'array.c', line 1953 static VALUE rb_ary_slice_bang(argc, argv, ary) int argc; |
#sort ⇒ Array #sort {|a, b| ... } ⇒ Array
Returns a new array created by sorting self. Comparisons for the sort will be done using the <=>
operator or using an optional code block. The block implements a comparison between a and b, returning -1, 0, or +1. See also Enumerable#sort_by
.
a = [ "d", "a", "e", "c", "b" ]
a.sort #=> ["a", "b", "c", "d", "e"]
a.sort {|x,y| y <=> x } #=> ["e", "d", "c", "b", "a"]
1676 1677 1678 |
# File 'array.c', line 1676 VALUE rb_ary_sort(ary) VALUE ary; |
#sort! ⇒ Array #sort! {|a, b| ... } ⇒ Array
Sorts self. Comparisons for the sort will be done using the <=>
operator or using an optional code block. The block implements a comparison between a and b, returning -1, 0, or +1. See also Enumerable#sort_by
.
a = [ "d", "a", "e", "c", "b" ]
a.sort #=> ["a", "b", "c", "d", "e"]
a.sort {|x,y| y <=> x } #=> ["e", "d", "c", "b", "a"]
1648 1649 1650 |
# File 'array.c', line 1648 VALUE rb_ary_sort_bang(ary) VALUE ary; |
#to_a ⇒ Array
Returns self. If called on a subclass of Array, converts the receiver to an Array object.
1476 1477 1478 |
# File 'array.c', line 1476 static VALUE rb_ary_to_a(ary) VALUE ary; |
#to_ary ⇒ Array
Returns self.
1495 1496 1497 |
# File 'array.c', line 1495 static VALUE rb_ary_to_ary_m(ary) VALUE ary; |
#to_s ⇒ String
Returns self.join
.
[ "a", "e", "i", "o" ].to_s #=> "aeio"
1341 1342 1343 |
# File 'array.c', line 1341 VALUE rb_ary_to_s(ary) VALUE ary; |
#transpose ⇒ Array
Assumes that self is an array of arrays and transposes the rows and columns.
a = [[1,2], [3,4], [5,6]]
a.transpose #=> [[1, 3, 5], [2, 4, 6]]
2121 2122 2123 |
# File 'array.c', line 2121 static VALUE rb_ary_transpose(ary) VALUE ary; |
#uniq ⇒ Array
Returns a new array by removing duplicate values in self.
a = [ "a", "a", "b", "b", "c" ]
a.uniq #=> ["a", "b", "c"]
2783 2784 2785 |
# File 'array.c', line 2783 static VALUE rb_ary_uniq(ary) VALUE ary; |
#uniq! ⇒ Array?
Removes duplicate elements from self. Returns nil
if no changes are made (that is, no duplicates are found).
a = [ "a", "a", "b", "b", "c" ]
a.uniq! #=> ["a", "b", "c"]
b = [ "a", "b", "c" ]
b.uniq! #=> nil
2750 2751 2752 |
# File 'array.c', line 2750 static VALUE rb_ary_uniq_bang(ary) VALUE ary; |
#unshift(obj, ...) ⇒ Array
Prepends objects to the front of array. other elements up one.
a = [ "b", "c", "d" ]
a.unshift("a") #=> ["a", "b", "c", "d"]
a.unshift(1, 2) #=> [ 1, 2, "a", "b", "c", "d"]
554 555 556 |
# File 'array.c', line 554 static VALUE rb_ary_unshift_m(argc, argv, ary) int argc; |
#values_at(selector, ...) ⇒ Array
Returns an array containing the elements in self corresponding to the given selector(s). The selectors may be either integer indices or ranges. See also Array#select
.
a = %w{ a b c d e f }
a.values_at(1, 3, 5)
a.values_at(1, 3, 5, 7)
a.values_at(-1, -3, -5, -7)
a.values_at(1..3, 2...5)
1793 1794 1795 |
# File 'array.c', line 1793 static VALUE rb_ary_values_at(argc, argv, ary) int argc; |
#zip(arg, ...) ⇒ Array #zip(arg, ...) {|arr| ... } ⇒ nil
Converts any arguments to arrays, then merges elements of self with corresponding elements from each argument. This generates a sequence of self.size
n-element arrays, where n is one more that the count of arguments. If the size of any argument is less than enumObj.size
, nil
values are supplied. If a block given, it is invoked for each output array, otherwise an array of arrays is returned.
a = [ 4, 5, 6 ]
b = [ 7, 8, 9 ]
[1,2,3].zip(a, b) #=> [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
[1,2].zip(a,b) #=> [[1, 4, 7], [2, 5, 8]]
a.zip([1,2],[8]) #=> [[4,1,8], [5,2,nil], [6,nil,nil]]
2071 2072 2073 |
# File 'array.c', line 2071 static VALUE rb_ary_zip(argc, argv, ary) int argc; |
#|(other_array) ⇒ Array
Set Union—Returns a new array by joining this array with other_array, removing duplicates.
[ "a", "b", "c" ] | [ "c", "d", "a" ]
#=> [ "a", "b", "c", "d" ]
2709 2710 2711 |
# File 'array.c', line 2709 static VALUE rb_ary_or(ary1, ary2) VALUE ary1, ary2; |