class

Array(T)

Inherits Comparable < Indexable::Mutable < Indexable < Enumerable < Iterable < Reference < Object

An Array is an ordered, integer-indexed collection of objects of type T.

Array indexing starts at 0. A negative index is assumed to be relative to the end of the array: -1 indicates the last element, -2 is the next to last element, and so on.

An Array can be created using the usual new method (several are provided), or with an array literal:

Array(Int32).new  # => []
[1, 2, 3]         # Array(Int32)
[1, "hello", 'x'] # Array(Int32 | String | Char)

See Array literals in the language reference.

An Array can have mixed types, meaning T will be a union of types, but these are determined when the array is created, either by specifying T or by using an array literal. In the latter case, T will be set to the union of the array literal elements' types.

When creating an empty array you must always specify T:

[] of Int32 # same as Array(Int32)
[]          # syntax error

An Array is implemented using an internal buffer of some capacity and is reallocated when elements are pushed to it when more capacity is needed. This is normally known as a dynamic array.

You can use a special array literal syntax with other types too, as long as they define an argless new method and a << method. Set is one such type:

set = Set{1, 2, 3} # => Set{1, 2, 3}
set.class          # => Set(Int32)

The above is the same as this:

set = Set(typeof(1, 2, 3)).new
set << 1
set << 2
set << 3

Class methods

wrap(object : Array(U)) : Array(U) forall U
Source
wrap(object : U) : Array(U) forall U
Source

Instance methods

blank?
Source
deep_dup
Source
excluding(*elements) : Array(T)
Source
extract!
Source
fifth
Source
find_sole
Source
fourth
Source
in_groups(number : Int, fill_with : Nil | Bool = nil) : Array(Array(T | Nil))
Source
in_groups_of(number : Int, fill_with : Nil = nil) : Array(Array(T | Nil))
Source
in_groups_of(number : Int, fill_with : V) : Array(Array(T | V)) forall V
Source
including(*elements : T) : Array(T)
Source
pick(key : Symbol)
Source
pick

pick() with no key/block: nil for empty (Ruby), TypeError otherwise (P0: single-key pick; multi-key pick is a later addition).

Source
pick
Source
pluck(key : Symbol) : Array
Source
pluck
Source
second
Source
second_to_last
Source
sole

Rails 8.1 semantics: sole IGNORES a passed block — it is "the only element of this array" (0 or >1 elements raise SoleItemExpectedError).

Source
sole
Source
split(pattern : T2) : Array(Array(T)) forall T2
Source
split
Source
third
Source
third_to_last
Source
to_param
Source
to_sentence(words_connector : String = ", ", two_words_connector : String = " and ", last_word_connector : String = ", and ") : String
Source