Ruby Array Functions

EditRocket provides the following information on Array functions in the Ruby source code builder.

abbrev(pattern = nil) - Calculates the set of unambiguous abbreviations for the strings in self. If passed a pattern or a string, only the strings matching the pattern or starting with the string are considered.

assoc(obj) - 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.

at(index) - 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.)

clear - Removes all elements from self.

collect! - Invokes the block once for each element of self, replacing the element with the value returned by block. See also Enumerable#collect.

collect - Invokes block once for each element of self. Creates a new array containing the values returned by the block. See also Enumerable#collect.

compact! - Removes nil elements from array. Returns nil if no changes were made.

compact - Returns a copy of self with all nil elements removed.

concat(other_array) - Appends the elements in other_array to self.

dclone() -

delete(obj) - 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.

delete_at(index) - Deletes the element at the specified index, returning that element, or nil if the index is out of range. See also Array#slice!.

delete_if - Deletes every element of self for which block evaluates to true.

each - Calls block once for each element in self, passing that element as a parameter.

each_index - Same as Array#each, but passes the index of the element instead of the element itself.

empty? - Returns true if self array contains no elements.

eql?(other) - Returns true if array and other are the same object, or are both arrays with the same content.

fetch(index) - 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.

fill(obj) - 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.

first(n) - 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.

flatten! - Flattens self in place. Returns nil if no modifications were made (i.e., array contains no subarrays.)

flatten - 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.

frozen? - Return true if this array is frozen (or temporarily frozen while being sorted).

hash - Compute a hash-code for this array. Two arrays with the same content will have the same hash code (and will compare using eql?).

include?(obj) - Returns true if the given object is present in self (that is, if any object == anObject), false otherwise.

index(obj) - Returns the index of the first object in self such that is == to obj. Returns nil if no match is found.

indexes( i1, i2, ... iN ) - Deprecated; use Array#values_at.

indices( i1, i2, ... iN ) - Deprecated; use Array#values_at.

initialize_copy array.replace(other_array) - Replaces the contents of self with the contents of other_array, truncating or expanding if necessary.

insert(index, obj...) - Inserts the given values before the element with the given index (which may be negative).

inspect - Create a printable version of array.

join(sep=$,) - Returns a string created by converting each element of the array to a string, separated by sep.

last(n) - Returns the last element(s) of self. If the array is empty, the first form returns nil.

length - Returns the number of elements in self. May be zero.

map! - Invokes the block once for each element of self, replacing the element with the value returned by block. See also Enumerable#collect.

map - Invokes block once for each element of self. Creates a new array containing the values returned by the block. See also Enumerable#collect.

new(size=0, obj=nil) - 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.

nitems - Returns the number of non-nil elements in self. May be zero.

pack( aTemplateString ) - 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.

pop - Removes the last element from self and returns it, or nil if the array is empty.

pretty_print(q) -

pretty_print_cycle(q) -

push(obj, ... ) - 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.

quote() -

rassoc(key) - 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.

reject! - 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.

reject - Returns a new array containing the items in self for which the block is not true.

replace(other_array) - Replaces the contents of self with the contents of other_array, truncating or expanding if necessary.

reverse! - Reverses self in place.

reverse - Returns a new array containing self's elements in reverse order.

reverse_each - Same as Array#each, but traverses self in reverse order.

rindex(obj) - Returns the index of the last object in array == to obj. Returns nil if no match is found.

select - 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 - Returns the first element of self and removes it (shifting all other elements down by one). Returns nil if the array is empty.

size() - "Alias for #length"

slice!(index) - "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:"

slice(index) - 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.

sort! - 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.

sort - 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.

to_a - Returns self. If called on a subclass of Array, converts the receiver to an Array object.

to_ary - Returns self.

to_s - Returns self.join.

to_yaml( opts = {} ) -

transpose - Assumes that self is an array of arrays and transposes the rows and columns.

uniq! - Removes duplicate elements from self. Returns nil if no changes are made (that is, no duplicates are found).

uniq - Returns a new array by removing duplicate values in self.

unshift(obj, ...) - Prepends objects to the front of array. other elements up one.

values_at(selector,... ) - 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.

yaml_initialize( tag, val ) -

zip(arg, ...) - 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.