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)

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 that is reallocated when elements are pushed to it and 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} # => [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:

array.cr
json/to_json.cr
yaml/to_yaml.cr

Class Method Summary

Instance Method Summary

Instance methods inherited from module Comparable(T)

<(other : T) <, <=(other : T) <=, <=>(other : T) <=>, ==(other : T) ==, >(other : T) >, >=(other : T) >=, between?(min, max) between?

Instance methods inherited from module Iterable

cycle
cycle(n)
cycle
, each each, each_cons(count : Int) each_cons, each_slice(count : Int) each_slice, each_with_index(offset = 0) each_with_index, each_with_object(obj) each_with_object

Instance methods inherited from module Enumerable(T)

all?
all?(&block)
all?
, any?
any?(&block)
any?
, compact_map(&block) compact_map, count(item)
count(&block)
count
, cycle(&block)
cycle(n, &block)
cycle
, each(&block : T -> _) each, each_cons(count : Int, &block) each_cons, each_slice(count : Int, &block) each_slice, each_with_index(offset = 0, &block) each_with_index, each_with_object(obj, &block) each_with_object, find(if_none = nil, &block) find, first first, first? first?, flat_map(&block : T -> Array(U)) flat_map, grep(pattern) grep, group_by(&block : T -> U) group_by, in_groups_of(size : Int, filled_up_with = nil, &block)
in_groups_of(size : Int, filled_up_with = nil)
in_groups_of
, includes?(obj) includes?, index(obj)
index(&block)
index
, index_by(&block : T -> U) index_by, join(separator = "")
join(separator, io, &block)
join(separator = "", &block)
join(separator, io)
join
, map(&block : T -> U) map, map_with_index(&block : T, Int32 -> U) map_with_index, max max, max? max?, max_by(&block : T -> U) max_by, max_by?(&block : T -> U) max_by?, max_of(&block : T -> U) max_of, max_of?(&block : T -> U) max_of?, min min, min? min?, min_by(&block : T -> U) min_by, min_by?(&block : T -> U) min_by?, min_of(&block : T -> U) min_of, min_of?(&block : T -> U) min_of?, minmax minmax, minmax? minmax?, minmax_by(&block : T -> U) minmax_by, minmax_by?(&block : T -> U) minmax_by?, minmax_of(&block : T -> U) minmax_of, minmax_of?(&block : T -> U) minmax_of?, none?(&block)
none?
none?
, one?(&block) one?, partition(&block) partition, reduce(&block)
reduce(memo, &block)
reduce
, reject(&block : T -> ) reject, select(&block : T -> ) select, size size, skip(count : Int) skip, skip_while(&block) skip_while, sum(initial, &block)
sum(&block)
sum(initial)
sum
sum
, take(count : Int) take, take_while(&block) take_while, to_a to_a, to_h to_h, to_set to_set

Instance methods inherited from class Reference

! !, ==(other)
==(other : self)
==
, hash hash, inspect(io : IO) : Nil inspect, nil? nil?, object_id object_id, same?(other : Nil)
same?(other : Reference)
same?
, to_s(io : IO) : Nil to_s

Instance methods inherited from class Object

! !, !=(other) !=, !~(other) !~, ==(other) ==, ===(other) ===, =~(other) =~, class class, clone clone, crystal_type_id crystal_type_id, dup dup, hash hash, inspect
inspect(io : IO)
inspect
, itself itself, not_nil! not_nil!, tap(&block) tap, to_json to_json, to_pretty_json(io : IO)
to_pretty_json
to_pretty_json
, to_s
to_s(io : IO)
to_s
, to_yaml(io : IO)
to_yaml
to_yaml
, try(&block) try

Class methods inherited from class Object

==(other : Class) ==, ===(other) ===, cast(other) : self cast, from_json(string_or_io) from_json, from_yaml(string : String) from_yaml, hash hash, inspect(io) inspect, name : String name, to_s(io) to_s

Class Method Detail

def self.build(capacity : Int, &block) #

Creates a new Array, allocating an internal buffer with the given capacity, and yielding that buffer. The block must return the desired size of the array.

This method is unsafe, but is usually used to initialize the buffer by passing it to a C function.

Array.build(3) do |buffer|
  LibSome.fill_buffer_and_return_number_of_elements_filled(buffer)
end

[View source]
def self.each_product(arrays, &block) #

[View source]
def self.each_product(*arrays : Array, &block) #

[View source]
def self.from_json(string_or_io, &block) #

[View source]
def self.from_yaml(string : String, &block) #

[View source]
def self.new(size : Int, &block : Int32 -> T) #

Creates a new Array of the given size and invokes the block once for each index of the array, assigning the block's value in that index.

Array.new(3) { |i| (i + 1) ** 2 } # => [1, 4, 9]

ary = Array.new(3) { [1] }
puts ary # => [[1], [1], [1]]
ary[0][0] = 2
puts ary # => [[2], [1], [1]]

[View source]
def self.new(pull : JSON::PullParser, &block) #

[View source]
def self.new(pull : JSON::PullParser) #

[View source]
def self.new(pull : YAML::PullParser, &block) #

[View source]
def self.new(pull : YAML::PullParser) #

[View source]
def self.product(arrays) #

[View source]
def self.product(*arrays : Array) #

[View source]
def self.new(size : Int, value : T) #

Creates a new Array of the given size filled with the same value in each position.

Array.new(3, 'a') # => ['a', 'a', 'a']

ary = Array.new(3, [1])
puts ary # => [[1], [1], [1]]
ary[0][0] = 2
puts ary # => [[2], [2], [2]]

[View source]
def self.new(initial_capacity : Int) #

Creates a new empty Array backed by a buffer that is initially initial_capacity big.

The initial_capacity is useful to avoid unnecessary reallocations of the internal buffer in case of growth. If you have an estimate of the maxinum number of elements an array will hold, you should initialize it with that capacity for improved execution performance.

ary = Array(Int32).new(5)
ary.size # => 0

[View source]
def self.new #

Creates a new empty Array.


[View source]

Instance Method Detail

def &(other : Array(U)) #

Set intersection: returns a new array containing elements common to the two arrays, excluding any duplicates. The order is preserved from the original array.

[1, 1, 3, 5] & [1, 2, 3]               # => [ 1, 3 ]
['a', 'b', 'b', 'z'] & ['a', 'b', 'c'] # => [ 'a', 'b' ]

See also: #uniq.


[View source]
def *(times : Int) #

Repetition: Returns a new array built by concatenating times copies of ary.

["a", "b", "c"] * 2 # => [ "a", "b", "c", "a", "b", "c" ]

[View source]
def +(other : Array(U)) #

Concatenation. Returns a new array built by concatenating two arrays together to create a third. The type of the new array is the union of the types of both the other arrays.

[1, 2] + ["a"]  # => [1,2,"a"] of (Int32 | String)
[1, 2] + [2, 3] # => [1,2,2,3]

[View source]
def -(other : Array(U)) #

Difference. Returns a new array that is a copy of the original, removing any items that appear in other. The order of the original array is preserved.

[1, 2, 3] - [2, 1] # => [3]

[View source]
def <<(value : T) #

Append. Alias for #push.

a = [1, 2]
a << 3 # => [1,2,3]

[View source]
def <=>(other : Array) #

Combined comparison operator. Returns 0 if the first array equals the second, 1 if the first is greater than the second and -1 if the first is smaller than the second.

It compares the elements of both arrays in the same position using the #<=> operator, as soon as one of such comparisons returns a non zero value, that result is the return value of the whole comparison.

If all elements are equal, the comparison is based on the size of the arrays.

[8] <=> [1, 2, 3] # => 1
[2] <=> [4, 2, 3] # => -1
[1, 2] <=> [1, 2] # => 0

[View source]
def ==(other : Array) #

Equality. Returns true if it is passed an Array and #equals? returns true for both arrays, the caller and the argument.

ary = [1, 2, 3]
ary == [1, 2, 3] # => true
ary == [2, 3]    # => false

[View source]
def [](index : Int) #

Returns the element at the given index.

Negative indices can be used to start counting from the end of the array. Raises IndexError if trying to access an element outside the array's range.

ary = ['a', 'b', 'c']
ary[0]  # => 'a'
ary[2]  # => 'c'
ary[-1] # => 'c'
ary[-2] # => 'b'

ary[3]  # raises IndexError
ary[-4] # raises IndexError

[View source]
def [](range : Range(Int, Int)) #

Returns all elements that are within the given range

Negative indices count backward from the end of the array (-1 is the last element). Aditionally, an empty array is returned when the starting index for an element range is at the end of the array.

Raises IndexError if the starting index is out of range.

a = ["a", "b", "c", "d", "e"]
a[1..3]    # => ["b", "c", "d"]
a[4..7]    # => ["e"]
a[6..10]   # => Index Error
a[5..10]   # => []
a[-2...-1] # => ["d"]

[View source]
def [](start : Int, count : Int) #

Returns count or less (if there aren't enough) elements starting at the given start index.

Negative indices count backward from the end of the array (-1 is the last element). Aditionally, an empty array is returned when the starting index for an element range is at the end of the array.

Raises IndexError if the starting index is out of range.

a = ["a", "b", "c", "d", "e"]
a[-3, 3] # => ["c", "d", "e"]
a[6, 1]  # => Index Error
a[1, 2]  # => ["b", "c"]
a[5, 1]  # => []

[View source]
def []=(index : Int, value : T) #

Sets the given value at the given index.

Negative indices can be used to start counting from the end of the array. Raises IndexError if trying to set an element outside the array's range.

ary = [1, 2, 3]
ary[0] = 5
p ary # => [5,2,3]

ary[3] = 5 # => IndexError

[View source]
def []=(index : Int, count : Int, values : Array(T)) #

Replaces a subrange with the elements of the given array.

a = [1, 2, 3, 4, 5]
a[1, 3] = [6, 7, 8]
a # => [1, 6, 7, 8, 5]

a = [1, 2, 3, 4, 5]
a[1, 3] = [6, 7]
a # => [1, 6, 7, 5]

a = [1, 2, 3, 4, 5]
a[1, 3] = [6, 7, 8, 9, 10]
a # => [1, 6, 7, 8, 9, 10, 5]

[View source]
def []=(range : Range(Int, Int), values : Array(T)) #

Replaces a subrange with the elements of the given array.

a = [1, 2, 3, 4, 5]
a[1..3] = [6, 7, 8]
a # => [1, 6, 7, 8, 5]

a = [1, 2, 3, 4, 5]
a[1..3] = [6, 7]
a # => [1, 6, 7, 5]

a = [1, 2, 3, 4, 5]
a[1..3] = [6, 7, 8, 9, 10]
a # => [1, 6, 7, 8, 9, 10, 5]

[View source]
def []=(range : Range(Int, Int), value : T) #

Replaces a subrange with a single value.

a = [1, 2, 3, 4, 5]
a[1..3] = 6
a # => [1, 6, 5]

a = [1, 2, 3, 4, 5]
a[1...1] = 6
a # => [1, 6, 2, 3, 4, 5]

[View source]
def []=(index : Int, count : Int, value : T) #

Replaces a subrange with a single value. All elements in the range index...index+count are removed and replaced by a single element value.

If count is zero, value is inserted at index.

Negative values of index count from the end of the array.

a = [1, 2, 3, 4, 5]
a[1, 3] = 6
a # => [1, 6, 5]

a = [1, 2, 3, 4, 5]
a[1, 0] = 6
a # => [1, 6, 2, 3, 4, 5]

[View source]
def []?(index : Int) #

Returns the element at the given index.

Negative indices can be used to start counting from the end of the array. Returns nil if trying to access an element outside the array's range.

ary = ['a', 'b', 'c']
ary[0]?  # => 'a'
ary[2]?  # => 'c'
ary[-1]? # => 'c'
ary[-2]? # => 'b'

ary[3]?  # nil
ary[-4]? # nil

[View source]
def at(index : Int) #

Returns the element at the given index, if in bounds, otherwise raises IndexError.

a = [:foo, :bar]
a.at(0) # => :foo
a.at(2) # => IndexError

[View source]
def at(index : Int, &block) #

Returns the element at the given index, if in bounds, otherwise executes the given block and returns its value.

a = [:foo, :bar]
a.at(0) { :baz } # => :foo
a.at(2) { :baz } # => :baz

[View source]
def clear #

Removes all elements from self.

a = ["a", "b", "c", "d", "e"]
a.clear # => []

[View source]
def clone #

Returns a new Array that has this array's elements cloned. That is, it returns a deep copy of this array.

Use #dup if you want a shallow copy.

ary = [[1, 2], [3, 4]]
ary2 = ary.clone
ary[0][0] = 5
puts ary  # => [[5, 2], [3, 4]]
puts ary2 # => [[1, 2], [3, 4]]

ary2 << [7, 8]
puts ary  # => [[5, 2], [3, 4]]
puts ary2 # => [[1, 2], [3, 4], [7, 8]]

[View source]
def combinations(size = self.size : Int) #

[View source]
def compact #

Returns a copy of self with all nil elements removed.

["a", nil, "b", nil, "c", nil].compact # => ["a", "b", "c"]

[View source]
def compact! #

Removes nil elements from this array.

ary = ["a", nil, "b", nil, "c"]
ary.compact!
ary # => ["a", "b", "c"]

[View source]
def concat(other : Enumerable) #

Appends the elements of other to self, and returns self.

ary = ["a", "b"]
ary.concat(["c", "d"])
ary # => ["a", "b", "c", "d"]

[View source]
def concat(other : Array) #

Appends the elements of other to self, and returns self.

ary = ["a", "b"]
ary.concat(["c", "d"])
ary # => ["a", "b", "c", "d"]

[View source]
def delete(obj) #

Deletes all items from self that are equal to obj.

a = ["a", "b", "b", "b", "c"]
a.delete("b")
a # => ["a", "c"]

[View source]
def delete_at(index : Int, count : Int) #

Deletes count or less (if there aren't enough) elements at the given start index, returning that elements. Raises IndexError if the index is out of range.

a = ["ant", "bat", "cat", "dog"]
a.delete_at(1, 2)  # => ["bat", "cat"]
a                  # => ["ant", "dog"]
a.delete_at(99, 1) # => IndexError

[View source]
def delete_at(index : Int) #

Deletes the element at the given index, returning that element. Raises IndexError if the index is out of range.

a = ["ant", "bat", "cat", "dog"]
a.delete_at(2)  # => "cat"
a               # => ["ant", "bat", "dog"]
a.delete_at(99) # => IndexError

[View source]
def delete_at(range : Range(Int, Int)) #

Deletes all elements that are within the given range, returning that elements. Raises IndexError if the index is out of range.

a = ["ant", "bat", "cat", "dog"]
a.delete_at(1..2)    # => ["bat", "cat"]
a                    # => ["ant", "dog"]
a.delete_at(99..100) # => IndexError

[View source]
def dup #

Returns a new Array that has exactly this array's elements. That is, it returns a shallow copy of this array.

Use #clone if you want a deep copy.

ary = [[1, 2], [3, 4]]
ary2 = ary.dup
ary[0][0] = 5
puts ary  # => [[5, 2], [3, 4]]
puts ary2 # => [[5, 2], [3, 4]]

ary2 << [7, 8]
puts ary  # => [[5, 2], [3, 4]]
puts ary2 # => [[5, 2], [3, 4], [7, 8]]

[View source]
def each(&block) #

Calls the given block once for each element in this array, passing that element as a parameter.

a = ["a", "b", "c"]
a.each { |x| print x, " -- " }

produces:

a -- b -- c --

[View source]
def each #

Returns an Iterator for the elements of this array.

a = ["a", "b", "c"]
iter = a.each
iter.next # => "a"
iter.next # => "b"

The returned iterator keeps a reference to this array: if the array changes, the returned values of the iterator change as well.


[View source]
def each_combination(size = self.size : Int, &block) #

[View source]
def each_combination(size = self.size : Int) #

[View source]
def each_index #

Returns an Iterator for each index in this this array.

a = ["a", "b", "c"]
iter = a.each_index
iter.next # => 0
iter.next # => 1

The returned iterator keeps a reference to this array: if the array changes, the returned values of the iterator change as well.


[View source]
def each_index(&block) #

Calls the given block once for each index in this array, passing that index as a parameter.

a = ["a", "b", "c"]
a.each_index { |x| print x, " -- " }

produces:

0 -- 1 -- 2 --

[View source]
def each_permutation(size = self.size : Int, &block) #

Yields each possible permutation of size #size of this array.

a = [1, 2, 3]
sums = [] of Int32
a.each_permutation(2) { |p| sums << p.sum } #=> [1, 2, 3]
sums #=> [3, 4, 3, 5, 4, 5]

[View source]
def each_permutation(size = self.size : Int) #

Returns an Iterator over each possible permutation of size #size of this array.

iter = [1, 2, 3].each_permutation
iter.next # => [1, 2, 3]
iter.next # => [1, 3, 2]
iter.next # => [2, 1, 3]
iter.next # => [2, 3, 1]
iter.next # => [3, 1, 2]
iter.next # => [3, 2, 1]
iter.next # => Iterator::Stop

[View source]
def each_repeated_combination(size = self.size : Int, &block) #

[View source]
def each_repeated_combination(size = self.size : Int) #

[View source]
def each_repeated_permutation(size = self.size : Int, &block) #

[View source]
def empty? #

Returns true if this array is empty, false otherwise.

([] of Int32).empty? # => true
([1]).empty?         # => false

[View source]
def equals?(other : Array, &block) #

Determines if this array equals other according to a comparison done by the given block.

If this array's size is the same as other's size, this method yields elements from this array and other in tandem: if the block returns true for all of them, this method returns true. Otherwise it returns false.

a = [1, 2, 3]
b = ["a", "ab", "abc"]
a.equals?(b) { |x, y| x == y.size } # => true
a.equals?(b) { |x, y| x == y }      # => false

[View source]
def fill(value : T) #

Replaces every element in this array with the given value. Returns self.

a = [1, 2, 3]
a.fill(9) # => [9, 9, 9]

[View source]
def fill(value : T, range : Range(Int, Int)) #

Replaces every element in the given range with the given value. Returns self.

Negative values of from count from the end of the array.

a = [1, 2, 3, 4, 5]
a.fill(9, 2..3) # => [1, 2, 9, 9, 5]

[View source]
def fill(range : Range(Int, Int), &block) #

Yields each index of this array, in the given range, to the given block and then assigns the block's value in that position. Returns self.

a = [1, 2, 3, 4, 5, 6]
a.fill(2..3) { |i| i * i } # => [1, 2, 4, 9, 5, 6]

[View source]
def fill(from : Int, &block) #

Yields each index of this array, starting from from, to the given block and then assigns the block's value in that position. Returns self.

Negative values of from count from the end of the array.

a = [1, 2, 3, 4]
a.fill(2) { |i| i * i } # => [1, 2, 4, 9]

[View source]
def fill(from : Int, count : Int, &block) #

Yields each index of this array, starting from from and just count times, to the given block and then assigns the block's value in that position. Returns self.

Negative values of from count from the end of the array.

a = [1, 2, 3, 4, 5, 6]
a.fill(2, 2) { |i| i * i } # => [1, 2, 4, 9, 5, 6]

[View source]
def fill(value : T, from : Int) #

Replaces every element starting from the given index from in this array with the given value. Returns self.

Negative values of from count from the end of the array.

a = [1, 2, 3, 4, 5]
a.fill(9, 2) # => [1, 2, 9, 9, 9]

[View source]
def fill(value : T, from : Int, count : Int) #

Replaces every element starting from the given index from and only count times, in this array with the given value. Returns self.

Negative values of from count from the end of the array.

a = [1, 2, 3, 4, 5]
a.fill(9, 2, 2) # => [1, 2, 9, 9, 5]

[View source]
def fill(&block) #

Yields each index of this array to the given block and then assigns the block's value in that position. Returns self.

a = [1, 2, 3, 4]
a.fill { |i| i * i } # => [0, 1, 4, 9]

[View source]
def first(&block) #

Returns the first element of this array, if it's not empty, or the block's value.

([1, 2, 3]).first { 4 }   # => 1
([] of Int32).first { 4 } # => 4

[View source]
def first #

Returns the first element of this array, if it's not empty, else raises IndexError.

([1, 2, 3]).first   # => 1
([] of Int32).first # => raises IndexError

[View source]
def first? #

Returns the first element of this array, if it's not empty, or nil.

([1, 2, 3]).first?   # => 1
([] of Int32).first? # => nil

[View source]
def flatten #

Returns a new array that is a one-dimensional flattening of self (recursively).

That is, for every element that is an array, extract its elements into the new array

s = [1, 2, 3]         # => [1, 2, 3]
t = [4, 5, 6, [7, 8]] # => [4, 5, 6, [7, 8]]
a = [s, t, 9, 10]     # => [[1, 2, 3], [4, 5, 6, [7, 8]], 9, 10]
a.flatten             # => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

[View source]
def hash #

Returns a hash code based on this array's size and elements.

See Object#hash.


[View source]
def insert(index : Int, object : T) #

Insert the given object before the element with the given index, shifting successive elements if any. Returns self.

Negative indices count from the end of the array.

a = ["a", "b", "c"]
a.insert(0, "x")  # => ["x", "a", "b", "c"]
a.insert(2, "y")  # => ["x", "a", "y", "b", "c"]
a.insert(-1, "z") # => ["x", "a", "y", "b", "c", "z"]

[View source]
def last(&block) #

Returns the last element of this array, if it's not empty, or the block's value.

([1, 2, 3]).last { 4 }   # => 3
([] of Int32).last { 4 } # => 4

[View source]
def last #

Returns the last element of this array, if it's not empty, else raises IndexError.

([1, 2, 3]).last   # => 3
([] of Int32).last # => raises IndexError

[View source]
def last? #

Returns the last element of this array, if it's not empty, or nil.

([1, 2, 3]).last?   # => 1
([] of Int32).last? # => nil

[View source]
def map(&block : T -> U) #

Optimized version of Enumerable#map.


[View source]
def map!(&block) #

Invokes the given block once for each element of this array, replacing the element with the value returned by the block. Returns self.

a = [1, 2, 3]
a.map! { |x| x * x }
a # => [1, 4, 9]

[View source]
def map_with_index(&block : T, Int32 -> U) #

Optimized version of Enumerable#map_with_index.


[View source]
def permutations(size = self.size : Int) #

Returns an Array with all possible permutations of the given size.

a = [1, 2, 3]
a.permutations    #=> [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
a.permutations(1) #=> [[1],[2],[3]]
a.permutations(2) #=> [[1,2],[1,3],[2,1],[2,3],[3,1],[3,2]]
a.permutations(3) #=> [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
a.permutations(0) #=> [[]]
a.permutations(4) #=> []

[View source]
def pop(&block) #

[View source]
def pop #

[View source]
def pop(n : Int) #

[View source]
def pop? #

[View source]
def product(ary, &block) #

[View source]
def product(ary : Array(U)) #

[View source]
def push(*values : T) #

Append multiple values. The same as #push, but takes an arbitrary number of values to push into the array. Returns self.


[View source]
def push(value : T) #

Append. Pushes one value to the end of the array, given that the type of the value is T (which might be a type or a union of types). This expression returns the array iself, so several of them can be chained. See #pop for the opposite effect.

a = ["a", "b"]
a.push("c") # => ["a", "b", "c"]
a.push(1)   # => Errors, because the array only accepts String

a = ["a", "b"] of (Int32 | String)
a.push("c") # => ["a", "b", "c"]
a.push(1)   # => ["a", "b", "c", 1]

[View source]
def reject!(&block) #

Equivalent to Array#reject, but makes modification on the current object rather that returning a new one. Returns nil if no changes were made.


[View source]
def repeated_combinations(size = self.size : Int) #

[View source]
def repeated_permutations(size = self.size : Int) #

[View source]
def replace(other : Array) #

[View source]
def reverse #

[View source]
def reverse! #

[View source]
def reverse_each(&block) #

[View source]
def reverse_each #

[View source]
def rindex(value) #

[View source]
def rindex(&block) #

[View source]
def rotate(n = 1) #

[View source]
def rotate!(n = 1) #

[View source]
def sample(n : Int, random = Random::DEFAULT) #

[View source]
def sample(random = Random::DEFAULT) #

Returns a random element from this array, using the given random number generator. Raises if this array is empty.

a = [1, 2, 3]
a.sample                # => 2
a.sample                # => 1
a.sample(Random.new(1)) # => 3

[View source]
def select!(&block) #

Equivalent to Array#select but makes modification on the current object rather that returning a new one. Returns nil if no changes were made


[View source]
def shift(&block) #

[View source]
def shift #

[View source]
def shift(n : Int) #

[View source]
def shift? #

[View source]
def shuffle(random = Random::DEFAULT) #

[View source]
def shuffle!(random = Random::DEFAULT) #

[View source]
def size #

Returns the number of elements in the array.

[:foo, :bar].size # => 2

[View source]
def sort(&block : T, T -> Int32) #

[View source]
def sort #

[View source]
def sort! #

[View source]
def sort!(&block : T, T -> Int32) #

[View source]
def sort_by(&block : T -> _) #

[View source]
def sort_by!(&block : T -> _) #

[View source]
def swap(index0, index1) #

[View source]
def to_a #

[View source]
def to_json(io) #

[View source]
def to_s(io : IO) #

[View source]
def to_unsafe : Pointer(T) #

Returns a pointer to the internal buffer where this array's elements are stored.

This method is unsafe because it returns a pointer, and the pointed data might eventually not be that of this array if the array grows and its internal buffer is realloced.

ary = [1, 2, 3]
ary.to_unsafe[0] # => 1

[View source]
def to_yaml(yaml : YAML::Generator) #

[View source]
def transpose #

Assumes that self is an array of array and transposes the rows and columns.

a = [[:a, :b], [:c, :d], [:e, :f]]
a.transpose # => [[:a, :c, :e], [:b, :d, :f]]
a           # => [[:a, :b], [:c, :d], [:e, :f]]

[View source]
def uniq(&block : T -> _) #

Returns a new array by removing duplicate values in self, using the block's value for comparison.

a = [{"student", "sam"}, {"student", "george"}, {"teacher", "matz"}]
a.uniq { |s| s[0] } # => [{"student", "sam"}, {"teacher", "matz"}]
a                   # => [{"student", "sam"}, {"student", "george"}, {"teacher", "matz"}]

[View source]
def uniq #

Returns a new array by removing duplicate values in self.

a = ["a", "a", "b", "b", "c"]
a.uniq # => ["a", "b", "c"]
a      # => [ "a", "a", "b", "b", "c" ]

[View source]
def uniq!(&block) #

Removes duplicate elements from self, using the block's value for comparison. Returns self.

a = [{"student", "sam"}, {"student", "george"}, {"teacher", "matz"}]
a.uniq! { |s| s[0] } # => [{"student", "sam"}, {"teacher", "matz"}]
a                    # => [{"student", "sam"}, {"teacher", "matz"}]

[View source]
def uniq! #

Removes duplicate elements from self. Returns self.

a = ["a", "a", "b", "b", "c"]
a.uniq! # => ["a", "b", "c"]
a       # => ["a", "b", "c"]

[View source]
def unshift(obj : T) #

[View source]
def update(index : Int, &block) #

[View source]
def values_at(*indexes : Int) #

Returns a tuple populated with the elements at the given indexes. Raises if any index is invalid.

["a", "b", "c", "d"].values_at(0, 2) # => {"a", "c"}

[View source]
def zip(other : Array(U)) #

[View source]
def zip(other : Array, &block) #

[View source]
def zip?(other : Array(U)) #

[View source]
def zip?(other : Array, &block) #

[View source]
def |(other_ary : Array(U)) #

Set union: returns a new array by joining ary with other_ary, excluding any duplicates and preserving the order from the original array.

["a", "b", "c"] | ["c", "d", "a"] # => [ "a", "b", "c", "d" ]

See also: #uniq.


[View source]