Pass the result of Array#find_index to Array#delete_at :
>> arr.delete_at(arr.find_index(3)) >> arr => [1, 1, 2, 2, 3, 4, 5]
find_index() will return the index of the array of the first element that matches its argument. delete_at() removes an element from the array at the specified index.
To prevent delete_at() raising a TypeError if the index is not found, you can use the && construct to assign the result of find_index() variable and use this variable in delete_at() if it is not nil . The right side && will not be executed at all if the left side is false or nil .
>> (i = arr.find_index(3)) && arr.delete_at(i) => 3 >> (i = arr.find_index(6)) && arr.delete_at(i) => nil >> arr => [1, 1, 2, 2, 3, 4, 5]
source share