Skip to content

End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

Array#delete_at

array.delete_at(index) → deleted_object or nil

Deletes an element from self, per the given Integer index.

When index is non-negative, deletes the element at offset index:

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

If index is too large, returns nil.

When index is negative, counts backward from the end of the array:

Example
a = [:foo, 'bar', 2]
a.delete_at(-2) # => "bar"
a # => [:foo, 2]

If index is too small (far from zero), returns nil.

See also #delete #delete_if #deconstruct #difference also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1