The elements of an array are arbitrary Ruby objects. Arrays are formed by placing comma separated list of expressions in square brackets like:
[1, 2, 3]
Object
Array[item...]
Creates a newly created array with arguments as elements.
new([size])
Creates a newly created array with internal buffer-size size. The default size is 16.
self[nth]
Retrieves the nth item from an array.
self[start..end]
Returns an array containing the objects from start to end, including both ends.
self[start, length]
Returns an array containing length items from start.
self[nth] = val
Changes the nth element of the array into val.
self[start..end] = val
Replace the items from start to end with val. The type of val will be converted into the Array
self[start, length] = val
Replace the length items from start with val. The type of val will be converted into the Array
Example:
ary = [1, 2, 3, 4, 5] ary[0..2] = [0, 0] # Changed to [0, 0, 4, 5] ary[1, 0] = [7] # Changed to [0, 7, 0, 4, 5]
self + other
The array concatenation.
self * times
The array repetition.
self - other
The array subtraction. Returns a new array contains elements of the original, except which belongs other also. Duplicate items will be removed.
self & other
Returns a new array which contains elements belong to both arrays. Duplicate items will be removed.
self | other
The array join. Returns a new array which contains elements belong to either self or other. Duplicate items will be removed.
self << obj
Append a new item with value obj to the end of the array. This method returns the array itself, so that can be chained like:
array << obj1 << obj2
assoc(key)
Searches in the array of arrays, looking for the array whose first
element equals (compared by ==
) to key.
clear
Makes the length of the array to zero.
concat(other)
Append a items in the other array to the end of the array.
compact
compact!
Removes all nil items.
delete(val)
Delete the item which matches to val. If the block supplied, it will be evaluated when no item matches to val.
delete_at(pos)
Removes an element at the position specified by the argument pos. Returns the removed element.
delete_if {...}
Removes elements, which evaluated result of the iterator block is true.
each {...}
The iterator which gives each item to the iterator block.
each_index {...}
Iterates over each index of the array elements, that is:
(0..ary.size).each {}
empty?
Returns true if the array is empty.
fill(val)
fill(val, start[, length])
fill(val, start..end)
Fill range of the array with val. If no range specified, whole array will be filled with val.
include?(val)
Returns true if the array contains val.
index(val)
Returns the index of the item which equals to val. If no item found, returns nil.
indexes(index_1,..., index_n)
Returns an array contains items at each index specified by each argument.
join([sep])
Joins the elements in the array into a single string with
fields separated by the value of sep, and returns the
string. If sep is not supplied, the value of
$,
is used as the
default.
length
size
Returns length of the array.
nitems
Returns the number of non-nil items.
pack(template)
Packs the array into a binary structure, returning the
string containing the structure. The template
is a
sequence of characters that give the order and type of values, as
follows:
a
- ASCII string(null padded)
A
- ASCII string(space padded)
b
- bit string(ascending bit order)
B
- bit string(descending bit order)
h
- hex string(low nibble first)
H
- hex string(high nibble first)
c
- char
C
- unsigned char
s
- short
S
- unsigned short
i
- int
I
- unsigned int
l
- long
L
- unsigned int
n
- short in "network" byte-order
N
- long in "network" byte-order
v
- short in "VAX" (little-endian) byte-order
V
- long in "VAX" (little-endian) byte-order
f
- single-precision float in the native format
d
- A double-precision float in the native format
u
- uuencoded string
x
- null byte
X
- back up a byte
@
- moves to absolute position
pop
Pops and returns the last value of the array, shortening the array by
one. If there is no element in the array, returns nil
.
push(obj...)
Appends obj to the last of the array.
rassoc(value)
Searches in the array of arrays, looking for the array whose second
element equals (compared by ==
) to value.
reverse!
Replace the items in reverse order.
reverse_each
The iterator which gives each item to the iterator block in reverse order.
shift
Removes and returns the first element of the array, and moves other elements down.
sort
sort {|a, b|...}
sort!
sort! {|a, b|...}
Sorts the array. If sort
is called as an iterator, it
evaluate its iterator block with two arguments, and use the result to
compare. Without the iterator block, it compares elements with the
operator <=>
. sort
returns a newly created
sorted array, and sort!
executes sort in place.
unshift(obj)
Inserts obj to the front of the array.