Name
Array — Array class
Synopsis
Array is
a class for an ordered collection of objects, indexed by integer. Any
kind of object may be stored in an Array.
Arrays grow as you add elements.
Included Module
Enumerable
Class Methods
Array[x...]Creates an array.
Array::new([size=0[,fill=nil]])Creates an array. Its
sizeand initial values may also be specified.Array::new(4, "foo") # => ["foo","foo","foo","foo"]
Instance Methods
Methods of the
Array class ending in ! modify
their receiver and return an array if modification took place,
otherwise nil. Methods without a
! return a modified copy of the array.
arr&arrayReturns an array of elements common to both arrays.
[1,3,5]|[1,2,3] # => [1,3] [1,3,5]|[2,4,6] # => [1,2,3,4,5,6]
arr|arrayReturns an array combining elements from both arrays.
[1,3,5]|[2,4,6] # => [1,2,3,4,5,6]
arr*nIf
nis an integer, returns a copy of array withncopies ofarrconcatenated to it. Ifnis a string, the equivalent ofarr.join(n)is performed.[5] * 3 # => [5, 5, 5]. ["foo", "bar"] * "-" # => "foo-bar"
arr+arrayReturns a copy of
arrwitharrayconcatenated to its end.arr-arrayReturns a new array that is a copy of
arr, removing any items inarray.[1, 2, 3, 4] - [2, 3] # => [1, 4]
arr<< itemAppends
itemtoarr.arr[n]References the nth element of
arr. Ifnis negative, it’s interpreted as an offset from the end ofarr.arr[n..m]arr[n,len]Returns a partial string.
arr[n]=itemarr[r..m]=arrayarr[r,len]=arrayAssigns item or
arrto the specified elements.arr = [0, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access