OneBite.Dev - Coding blog in a bite size

remove item from array in Ruby

Code snippet on how to remove item from array in Ruby

  array = [1, 2, 3, 4]
  array.delete_at(2)

This code starts with an array of four elements. It then calls the .delete_at() method on the array, passing the index (position) of the item we want to remove as an argument. In this example, the argument is “2”, which is the third item in the array (indexes start from 0). The .delete_at() method will then delete the item at the specified index, returning the deleted element as a result. In this case, the 3 will be removed from the array and the deleted element will be returned.

ruby