Menu

Toshimaru's Blog

Remove element from Array in Ruby

delete(obj)

Remove elements from Array.

ary = [1, 2, 3, 4, 5, 3]
ary.delete(3)
ary #=> [1, 2, 4, 5]

delete_at(index)

Remove element at index position from Array.

ary = [1, 2, 3, 4, 5, 3]
ary.delete_at(2)
ary #=> [1, 2, 4, 5, 3]

pop & shift

  • pop: Remove the last element from Array
  • shift: Remove the first element from Array
ary = [1, 2, 3, 4, 5]
ary.pop
ary #=> [1, 2, 3, 4]

ary.shift
ary #=> [2, 3, 4]

Reference

Load more