The Array class is a container class for an ordered collection of objects, indexed by an integer. Any kind of object may be stored within an Array. Arrays grow as you add elements.
The base class of Array is Object.
Array.new(Number size, Object initial)
Constructs a new Array, optionally with a size and initial value.
Example
|
|
| Number |
% (Object value) Compares value to the receiver array. |
| Array |
+ (Object value) Returns new array containing value concatenated to receiver array. |
| Boolean |
= (Object value) Returns true if receiver array is equal to value. |
| Boolean |
!= (Object value) Returns true if receiver array is not equal to value. |
|
Object Array |
[] (Number index) [] (Range range) References specified elements in receiver array. |
| Object |
[]= (Number index, Object value) Assigns index specified array element to value. |
| Array |
add (Object value...) Appends value arguments to the end of the array. |
| Array |
clear () Clears the contents of the receiver array. |
| Array | compact () Removes nil values from copy of receiver array. |
| Array | compact_self () Removes nil values from receiver array. |
| Array |
concat (Object value) Returns new array containing value concatenated to receiver array. |
| Object | each (Method method) Calls method for each element in array, with element as the parameter. |
| Object | first () Returns first element in receiver array. |
| Number |
hash () Returns hash of receiver array. |
| Array |
insert (Number offset, Object value...) Inserts value into receiver array, starting at offset. |
| Boolean |
is_empty () Tests if array contains zero elements. |
| String |
join (Object delimiter) Return string created by converting each array element to a string, optionally delimited by delimiter. |
| Object | last () Returns last element in receiver array. |
| Number |
length () Returns the number of elements in the receiver array. |
| Object |
max () Returns the maximum value from array, or nil if array is empty. |
| Object |
min () Returns the minimum value from array, or nil if array is empty. |
| String |
pack (template) Packs the contents of array into a binary sequence according to the template directives. |
| Object |
pop () Removes last element from array and returns it, or nil if array is empty. |
| Array |
prepend (Object value...) Prepends value to receiver array. |
| Array |
push (Object value...) Appends value arguments to the end of the array. |
| Object |
remove (Object value) Removes items from receiver array that are equal to value. |
| Object |
remove_at (Number index, Number count) Removes count items starting at position index from receiver array. |
| Array |
replace (Array array) Replaces contents of receiver array with array. |
| Array |
reverse () Reverses the order of elements in copy of receiver array. |
| Array |
reverse_self () Reverses the order of elements in receiver array. |
| Array |
segment (Number length, Number offset) Extracts length elements from receiver array, optionally starting from offset. |
| Object |
shift () Removes and returns first element of receiver array (shifting other elements down by one). |
| Array |
sort () Returns a new array created by sorting the receiver array. |
| Array |
sort_self () Sorts the receiver array inplace. |
| Array |
to_array () Returns reciever array. |
| String |
to_string () Return new string created by converting each array element to a string. |
| Array |
transpose () Returns new array created by transposing receiver array. |
Number %(Object value)
Compares value to array. Returns -1 if array is less than, 0 if array is equal to, and +1 if array is greater than value.
Example
|
|
Array +(Object value)
Returns array containing value concatenated to array.
Example
|
|
Boolean =(Object value)
Returns true if array is equal to value, otherwise false.
Example
|
|
Boolean !=(Object value)
Returns true if array is not equal to value, otherwise false.
Example
|
|
Array [](Number index) Array [](Range range)
References elements specified by index or range in array.
Example
|
|
Object []=(Object value)
Assigns index specified element in array to value.
Example
|
|
Array add(Object value...)
Appends value arguments to the end of array.
Example
|
|
Array clear()
Clears the contents of array.
Example
|
|
Array compact()
Removes nil values from copy of array.
Example
|
|
Array compact_self()
Removes nil values from array.
Example
|
|
Array concat(Object value)
Returns array containing value concatenated to array.
Example
|
|
Object each(Method method)
Calls method for each element in array, with element as the parameter. Returns the last element value, or nil if array is empty.
Example
|
|
Object first()
Returns first element in array.
Example
|
|
Number hash()
Returns the hash of array.
Array insert(Number offset,
Object value...)
Inserts value into array, starting at offset.
Example
|
|
Boolean is_empty()
Tests if array contains zero elements.
Example
|
|
String join(Object delimiter)
Return string created by converting each array element to a string, optionally delimited by delimiter.
Example
|
|
Object last()
Returns last element in array.
Example
|
|
Number length()
Returns the number of elements in array.
Object max()
Returns the maximum value from array, or nil if array is empty.
Example
|
|
Object min()
Returns the minimum value from array, or nil if array is empty.
Example
|
|
Object pack(String template)
Packs the contents of array into a binary sequence according to the template directives.
Packed strings can be unpacked using the String method unpack.
The template string may contain:
| Directive | Description |
| z | Zero-terminated string |
| p | String preceded by length byte |
| P | String preceded by length word |
| a | String preceded by length double word |
| A | String |
| f | Float |
| d | Double |
| n | Nexus number |
| c | Character |
| b | Byte |
| h | 2 byte number |
| H | Unsigned 2 byte number |
| i | 4 byte number |
| I | Unsigned 4 byte number |
| l | 8 byte number |
| L | Unsigned 8 byte number |
| < | Little endian |
| > | Big endian |
| = | Native endian |
Example
|
|
Object pop()
Removes last element from array and returns it, or nil if array is empty.
Example
|
|
Array prepend(Object value...)
Prepends value to array, and returns array.
Example
|
|
Array push(Object value...)
Appends value arguments to the end of array.
Example
|
|
Object remove(Object value)
Removes items from array that are equal to value. Returns value or nil if item is not found.
Example
|
|
Object remove_at(Number index,
Number count)
Removes count items starting at position index from receiver array. If optional count argument is omitted the index element is removed.
Returns value of removed element, or array of removed elements if count is greater than one, or nil if index does not exist.
Example
|
|
Array replace(Array array)
Replaces contents of array with array.
Example
|
|
Array reverse()
Returns a new array of array elements in reverse order.
Example
|
|
Array reverse_self()
Reverses the order of elements in array.
Example
|
|
Array segment(Number length,
Number offset)
Extracts length elements from array, optionally starting from offset. The default offset is 0.
Example
|
|
Object shift()
Removes and returns first element of array (shifting other elements down by one).
Example
|
|
Array sort()
Returns a new array created by sorting the receiver array.
Sort comparisons are done using the % (comparison) operator. The sort implements the quicksort algorithm.
Example
|
|
Array sort_self()
Sorts the receiver array inplace.
Sort comparisons are done using the % (comparison) operator.
Example
|
|
Array to_array()
Returns array.
String to_string()
Return string created by converting each array element to a string.
Example
|
|
Array transpose()
Returns new array created by transposing array.
Example
|
|