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

Instance methods

append(*values : T) : self
Source
assoc(key)
Source
chunk_while

Same as each.chunk_while(reuse, &block).

See also: Iterator#chunk_while.

Source
combination(n : Int) : Array(Array(T))

Ruby combination(n) → массив всех сочетаний.

Source
difference(*others : Array) : Array(T)
Source
drop_while
Source
fetch(index : Int)

fetch(index) без значения по умолчанию — IndexError как в Ruby (диапазон в сообщении — от -size до size, как в Ruby: -3...3).

Source
filter_map
Source
flatten(depth : Int)

flatten(depth): ограничение глубины. Тип результата — Array(Object): рекурсивные союзы типов не выражаются в Crystal (см. DIFFERENCES.md).

Source
intersection(*others : Array) : Array(T)

Ruby set-операции: intersection/union/difference с несколькими аргументами.

Source
max(n : Int) : Array(T)

Returns an array of the maximum count elements, sorted descending.

It compares using <=> so it will work for any type that supports that method.

[7, 5, 2, 4, 9].max(3)                 # => [9, 7, 5]
%w[Eve Alice Bob Mallory Carol].max(2) # => ["Mallory", "Eve"]

Returns all elements sorted descending if count is greater than the number of elements in the source.

Raises Enumerable::ArgumentError if count is negative or if any elements are not comparable.

Source
min(n : Int) : Array(T)

Returns an array of the minimum count elements, sorted ascending.

It compares using <=> so it will work for any type that supports that method.

[7, 5, 2, 4, 9].min(3)                 # => [2, 4, 5]
%w[Eve Alice Bob Mallory Carol].min(2) # => ["Alice", "Bob"]

Returns all elements sorted ascending if count is greater than the number of elements in the source.

Raises Enumerable::ArgumentError if count is negative or if any elements are not comparable.

Source
pack(fmt : String) : String

Ruby Array#pack.

Source
permutation(n : Int) : Array(Array(T))
Source
prepend(*values : T) : self
Source
rassoc(value)
Source
sample(n : Int, random : Ruby::V4::Random) : Array(T)

sample(n) в порядке Ruby (rb_ary_sample): j = RAND_UPTO(len-i) из сжимающегося множества — фактический индекс = j + число выбранных ≤ j.

Source
sample(random : Ruby::V4::Random) : T | Nil

sample в порядке Ruby (rb_ary_sample): один элемент — limited(len-1).

Source
shuffle(random : Ruby::V4::Random) : Array(T)

Fisher-Yates в порядке Ruby (rb_ary_shuffle) поверх MT19937.

Source
slice(range : Range(Int, Int | Nil)) : Array(T)

Ruby Array#slice(range): ["a","b","c"].slice(1..2) → ["b","c"].

Source
slice_when

Same as each.slice_when(reuse, &block).

See also: Iterator#slice_when.

Source
take_while

Passes elements to the block until the block returns a falsey value, then stops iterating and returns an Array of all prior elements.

[1, 2, 3, 4, 5, 0].take_while { |i| i < 3 } # => [1, 2]
Source
union(*others : Array) : Array(T)
Source
values_at(*ranges : Range(Int, Int)) : Array(T)
Source