neumachen
11/21/2014 - 3:36 PM

Selecting an item in an array and recursively deleting it.

Selecting an item in an array and recursively deleting it.

#array you are working with
@arr = ["cat", "dog", "bird", "rabbit", "alligator", "mouse"]

#Method to print out array with indexs for easy selection
def display_arr
  @arr.each_with_index do |x, index|
    puts "#{index} - #{x}"
  end
end

#run display method
display_arr

#insert items to delete
puts "What should be deleted?"
@del = gets.chomp.split","

#run through the values of the @del array subtract the @del value from the index to keep the removal of items in order.
@del.each_with_index do |x, index|
  n = x.to_i - index
  @arr.delete_at n
end

#print the array out to see changes
puts "Your new array is : "
display_arr