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

fifth

Returns the fifth element in an array.

[1, 2, 3, 4, 5].second
=> [5]
Source
fourth

Returns the fourth element in an array.

[1, 2, 3, 4, 5].second
=> [4]
Source
from(position : Int)

Returns the tail of the array from position.

  %w( a b c d ).from(0)
=> ["a", "b", "c", "d"]

  %w( a b c d ).from(2)
=> ["c", "d"]

  %w( a b c d ).from(10)
=> []

  %w().from(0)
=> []

  %w( a b c d ).from(-2)
=> ["c", "d"]

  %w( a b c ).from(-10)
=> []
Source
in_groups(number, fill_with = nil)

Splits or iterates over the array in number of groups, padding any remaining slots with fill_with unless it is false.

  %w(1 2 3 4 5 6 7 8 9 10).in_groups(3) {|group| p group}
  ["1", "2", "3", "4"]
  ["5", "6", "7", nil]
  ["8", "9", "10", nil]

  %w(1 2 3 4 5 6 7 8 9 10).in_groups(3, '&nbsp;') {|group| p group}
  ["1", "2", "3", "4"]
  ["5", "6", "7", "&nbsp;"]
  ["8", "9", "10", "&nbsp;"]

  %w(1 2 3 4 5 6 7).in_groups(3, false) {|group| p group}
  ["1", "2", "3"]
  ["4", "5"]
  ["6", "7"]
Source
in_groups(number, fill_with = nil, &)
Source
not_last

Returns the elements in an array except the last one.

[1, 2, 3, 4, 5].not_last
=> [1, 2, 3, 4]
Source
rest

Returns the elements in an array except the first one.

[1, 2, 3, 4, 5].rest
=> [2, 3, 4, 5]
Source
second

Returns the second element in an array.

[1, 2, 3, 4, 5].second
=> [2]
Source
slice(index : Int, length : Int) : Array(T)

Array#slice(index : Int, length : Int)

Returns a sub array starting from the index for the length thats passed.

Source
slice(index : Int) : T

Array#slice(index : Int)

Returns the element of the array at the passed in Index.

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

Array#slice(range : Range(Int, Int))

Returns a sub array based on the range passed in.

Source
split(value = nil)

Divides the array into one or more subarrays based on a delimiting value or the result of an optional block.

[1, 2, 3, 4, 5].split(3)
=> [[1, 2], [4, 5]]
Source
split

Divides the array into one or more subarrays based on a the result of an optional block.

(1..10).to_a.split { |i| i % 3 == 0 }
=> [[1, 2], [4, 5], [7, 8], [10]]
Source
third

Returns the third element in an array.

[1, 2, 3, 4, 5].second
=> [3]
Source
to(position : Int)

Returns the beginning of the array up to position.

  %w( a b c d ).to(0)
=> ["a"]

  %w( a b c d ).to(2)
=> ["a", "b", "c"]

  %w( a b c d ).to(10)
=> ["a", "b", "c", "d"]

  %w().to(0)
=> []

  %w( a b c d ).to(-2)
=> ["a", "b", "c"]

  %w( a b c ).to(-10)
=> []
Source
to_param

Calls to_param on all its elements and joins the result with slashes.

source: Rails ActiveSupport

Source
to_query(namespace)

Cast an array as a HTTP::Params and buils an url-encode string.

["michael", "jhon"].to_query("user")
=> "user%5B%5D=michael&user%5B%5D=jhon"
Source