Description

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.


Constructor

Array.new(Number size, Object initial)

Constructs a new Array, optionally with a size and initial value.

Example

Array.new()
Array.new(2)
Array.new(2, "A")
{}
{"",""}
{"A","A"}


Methods

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

{1,2,3} % {1,2,3}
{1,2,3} % {4,5,6}
{4,5,6} % {1,2,3}
0
-1
1


+

Array +(Object value)

Returns array containing value concatenated to array.

Example

{1,2,3} + {4,5,6}
{1,2,3,4,5,6}


=

Boolean =(Object value)

Returns true if array is equal to value, otherwise false.

Example

{1,2,3} = {1,2,3}
true


!=

Boolean !=(Object value)

Returns true if array is not equal to value, otherwise false.

Example

{1,2,3} != {4,5,6}
true


[]

Array [](Number index)
Array [](Range range)

References elements specified by index or range in array.

Example

array = {1,2,3}
array[0]
str[1..2]
{1,2,3}
1
{2,3}


[]=

Object []=(Object value)

Assigns index specified element in array to value.

Example

array = {1,2,3}
array[0] = 4
array
{1,2,3}
72
{4,2,3}


add

Array add(Object value...)

Appends value arguments to the end of array.

Example

{1,2,3}.add("hello")
{1,2,3}.add(4,5,6)
{1,2,3,"hello"}
{1,2,3,4,5,6}


clear

Array clear()

Clears the contents of array.

Example

array = {1,2,3}
array.clear()
{1,2,3}
{}


compact

Array compact()

Removes nil values from copy of array.

Example

array = {1,2,nil,3}
array.compact()
array
{1,2,nil,3}
{1,2,3}
{1,2,nil,3}


compact_self

Array compact_self()

Removes nil values from array.

Example

array = {1,2,nil,3}
array.compact_self()
array
{1,2,nil,3}
{1,2,3}
{1,2,3}


concat

Array concat(Object value)

Returns array containing value concatenated to array.

Example

{1,2,3}.concat({4,5,6})
{1,2,3,4,5,6}


each

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

array = {1,2,3}
array.each(mth)
{1,2,3}
3


first

Object first()

Returns first element in array.

Example

array = {1,2,3}
array.first()
{1,2,3}
1


hash

Number hash()

Returns the hash of array.


insert

Array insert(Number offset,
             Object value...)

Inserts value into array, starting at offset.

Example

{2,3,4}.insert(0, 1)
{1,2,5}.insert(2, 3, 4)
{1,2,3,4}
{1,2,3,4,5}


is_empty

Boolean is_empty()

Tests if array contains zero elements.

Example

array = {}
array.is_empty()
{}
true


join

String join(Object delimiter)

Return string created by converting each array element to a string, optionally delimited by delimiter.

Example

{1,2,3}.join()
{1,2,3}.join("-")
"123"
"1-2-3"


last

Object last()

Returns last element in array.

Example

array = {1,2,3}
array.last()
{1,2,3}
3


length

Number length()

Returns the number of elements in array.


max

Object max()

Returns the maximum value from array, or nil if array is empty.

Example

array = {1,2,3}
array.max()
{1,2,3}
3


min

Object min()

Returns the minimum value from array, or nil if array is empty.

Example

array = {1,2,3}
array.min()
{1,2,3}
1


pack

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

string = {1,2,3}.pack("bbb")
string.unpack("bbb")
string
{1,2,3}


pop

Object pop()

Removes last element from array and returns it, or nil if array is empty.

Example

array = {1,2,3}
array.pop()
array
{1,2,3}
3
{1,2}


prepend

Array prepend(Object value...)

Prepends value to array, and returns array.

Example

array = {1,2,3}
array.prepend(10)
array.prepend(11,12)
{1,2,3}
{10,1,2,3}
{11,12,10,1,2,3}


push

Array push(Object value...)

Appends value arguments to the end of array.

Example

{1,2,3}.push("hello")
{1,2,3}.push(4,5,6)
{1,2,3,"hello"}
{1,2,3,4,5,6}


remove

Object remove(Object value)

Removes items from array that are equal to value. Returns value or nil if item is not found.

Example

array = {1,2,1,3}
array.remove(1)
array
{1,2,1,3}
1
{2,3}


remove_at

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 = {1,2,3}
array.remove_at(1)
array
{1,2,3}
2
{1,3}


replace

Array replace(Array array)

Replaces contents of array with array.

Example

array = {1,2,3}
array.replace({4,5,6})
array
{1,2,3}
{4,5,6}
{4,5,6}


reverse

Array reverse()

Returns a new array of array elements in reverse order.

Example

array = {1,2,3}
array.reverse
array
{1,2,3}
{3,2,1}
{1,2,3}


reverse_self

Array reverse_self()

Reverses the order of elements in array.

Example

array = {1,2,3}
array.reverse_self()
array
{1,2,3}
{3,2,1}
{3,2,1}


segment

Array segment(Number length,
              Number offset)

Extracts length elements from array, optionally starting from offset. The default offset is 0.

Example

{1,2,3,4,5,6}.segment(3)
{1,2,3,4,5,6}.segment(3, 2)
{1,2,3}
{3,4}


shift

Object shift()

Removes and returns first element of array (shifting other elements down by one).

Example

array = {1,2,3}
array.shift()
array
{1,2,3}
1
{2,3}


sort

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 = {4,2,1,3}
array.sort()
array
{4,2,1,3}
{1,2,3,4}
{4,2,1,3}


sort_self

Array sort_self()

Sorts the receiver array inplace.

Sort comparisons are done using the % (comparison) operator.

Example

array = {4,2,1,3}
array.sort_self()
array
{4,2,1,3}
{1,2,3,4}
{1,2,3,4}


to_array

Array to_array()

Returns array.


to_string

String to_string()

Return string created by converting each array element to a string.

Example

{1,2,3}.to_string()
"123"


transpose

Array transpose()

Returns new array created by transposing array.

Example

{{1,2,3},{4,5,6}}.transpose()
{{1,3},{2,4},{3,5}}