class Array(T)
Overview
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
Included Modules
Defined in:
crystal_on_steroids/array.crcrystal_on_steroids/dig.cr
crystal_on_steroids/to_query.cr
Instance Method Summary
- #fifth
- #fourth
-
#rest
Returns the elements in an array except the first one.
- #second
-
#split(value = nil)
Divides the array into one or more subarrays based on a delimiting +value+ or the result of an optional block.
-
#split(&)
Divides the array into one or more subarrays based on a the result of an optional block.
- #third
-
#to_param
Calls
#to_paramon all its elements and joins the result with slashes. -
#to_query(key)
Cast an array as a HTTP::Params and buils an url-encode string.
Macro Summary
Instance methods inherited from module Dig
dig(index)dig(index, *keys) dig
Instance methods inherited from module Enumerable(T)
avg
avg,
excludes?(obj)
excludes?,
many?(&)many? many?, pluck(*keys) pluck, without(*items) without
Instance methods inherited from class Object
blank?
blank?,
dig?(index, *keys)dig? dig?, in?(another_object) in?, presence presence, presence_in(another_object) presence_in, present? present?, to_param to_param, to_query(key)
to_query to_query
Instance Method Detail
Returns the elements in an array except the first one.
[1, 2, 3, 4, 5].rest
=> [2, 3, 4, 5]
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]]
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]]
Calls #to_param on all its elements and joins the result with
slashes.
source: Rails ActiveSupport
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"