Open the Array class and add a new method for this.
class Array def delete_at_multi(arr) arr = arr.sort.reverse # delete highest indexes first. arr.each do |i| self.delete_at i end self end end arr = ["a", "b", "c"] set = [1, 2] arr.delete_at_multi(set) arr # => ["a"]
This could, of course, be written as a stand-alone method if you do not want to reopen the class. Make sure that indexes in the reverse order are very important, otherwise you will reposition the elements later in the array to be deleted.
source share