OneBite.Dev - Coding blog in a bite size

remove a specific element from an array in Ruby

Code snippet on how to remove a specific element from an array in Ruby

arr = [1, 2, 3, 4, 5]
arr.delete(3)

This code deletes a specified element from an array. First, it declares a variable called arr and assigns an array of numbers from 1 to 5. Then, it calls the delete method on the arr array, passing the number 3 as an argument. This invoke the delete method, which will find any occurrence of the number 3, remove it from the array and return the element that was removed. As a result, arr now contains the numbers 1, 2, 4, and 5.

ruby