Skip to content
instance method

Array#at

(::int index) -> Elem
at(index) → object or nil

Returns the element of self specified by the given index or nil if there is no such element; index must be an integer-convertible object.

For non-negative index, returns the element of self at offset index:

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

For negative index, counts backwards from the end of self:

Example
a.at(-2) # => "bar"

Related: Array#[]; see also Methods for Fetching.

See also #assoc #bsearch #append #bsearch_index also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0