Skip to content
instance method

Array#[]

(::int index) -> Elem
(::int start, ::int length) -> ::Array[Elem]?
(::Range[::Integer?] range) -> ::Array[Elem]?
self[index] → object or nil
self[start, length] → object or nil
self[range] → object or nil
self[aseq] → object or nil

Returns elements from self; does not modify self.

In brief:

Example
a = [:foo, 'bar', 2]

# Single argument index: returns one element.
a[0]     # => :foo          # Zero-based index.
a[-1]    # => 2             # Negative index counts backwards from end.

# Arguments start and length: returns an array.
a[1, 2]  # => ["bar", 2]
a[-2, 2] # => ["bar", 2]    # Negative start counts backwards from end.

# Single argument range: returns an array.
a[0..1]  # => [:foo, "bar"]
a[0..-2] # => [:foo, "bar"] # Negative range-begin counts backwards from end.
a[-2..2] # => ["bar", 2]    # Negative range-end counts backwards from end.

When a single integer argument index is given, returns the element at offset index:

Example
a = [:foo, 'bar', 2]
a[0] # => :foo
a[2] # => 2
a # => [:foo, "bar", 2]

If index is negative, counts backwards from the end of self:

Example
a = [:foo, 'bar', 2]
a[-1] # => 2
a[-2] # => "bar"

If index is out of range, returns nil.

When two Integer arguments start and length are given, returns a new array of size length containing successive elements beginning at offset start:

Example
a = [:foo, 'bar', 2]
a[0, 2] # => [:foo, "bar"]
a[1, 2] # => ["bar", 2]

If start + length is greater than self.length, returns all elements from offset start to the end:

Example
a = [:foo, 'bar', 2]
a[0, 4] # => [:foo, "bar", 2]
a[1, 3] # => ["bar", 2]
a[2, 2] # => [2]

If start == self.size and length >= 0, returns a new empty array.

If length is negative, returns nil.

When a single Range argument range is given, treats range.min as start above and range.size as length above:

Example
a = [:foo, 'bar', 2]
a[0..1] # => [:foo, "bar"]
a[1..2] # => ["bar", 2]

Special case: If range.start == a.size, returns a new empty array.

If range.end is negative, calculates the end index from the end:

Example
a = [:foo, 'bar', 2]
a[0..-1] # => [:foo, "bar", 2]
a[0..-2] # => [:foo, "bar"]
a[0..-3] # => [:foo]

If range.start is negative, calculates the start index from the end:

Example
a = [:foo, 'bar', 2]
a[-1..2] # => [2]
a[-2..2] # => ["bar", 2]
a[-3..2] # => [:foo, "bar", 2]

If range.start is larger than the array size, returns nil.

Example
a = [:foo, 'bar', 2]
a[4..1] # => nil
a[4..0] # => nil
a[4..-1] # => nil

When a single Enumerator::ArithmeticSequence argument aseq is given, returns an array of elements corresponding to the indexes produced by the sequence.

Example
a = ['--', 'data1', '--', 'data2', '--', 'data3']
a[(1..).step(2)] # => ["data1", "data2", "data3"]

Unlike slicing with range, if the start or the end of the arithmetic sequence is larger than array size, throws RangeError.

Example
a = ['--', 'data1', '--', 'data2', '--', 'data3']
a[(1..11).step(2)]
# RangeError (((1..11).step(2)) out of range)
a[(7..).step(2)]
# RangeError (((7..).step(2)) out of range)

If given a single argument, and its type is not one of the listed, tries to convert it to Integer, and raises if it is impossible:

Example
a = [:foo, 'bar', 2]
# Raises TypeError (no implicit conversion of Symbol into Integer):
a[:foo]

Related: see Methods for Fetching.

Ruby version changes

  1. 3.0
See also .[] #[]= #== #<=> also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0