I’m doing my first program, a simple to-do list. I want it to let me type a number, and delete the corresponding item from the list.
Every time though, I get “no implicit conversion from nil to integer”. I can’t seem to work it out. Any ideas?
$list = Array.new
def mainmethod
puts "Enter new item, or type 'view' to view the list, 'delete' to delete items"
input = gets.chomp
if input.downcase == "view"
puts "Your to do list is as follows:"
puts $list
elsif input.downcase == "delete"
puts "Which item would you like to delete? (Enter a number)"
deletenumber = gets.chomp.to_i
deletenumber-=1
delete_list = [deletenumber]
delete_list.each do |del|
$list.delete_at($list.index(del))
end
else
$list << input
puts "Added to list!"
end
end
loop { mainmethod }
See http://www.ruby-doc.org/core-1.9.3/Array.html#method-i-delete_at for the proper way of using
Array#delete_at.What you do is calling
#indexwith a number that your array does not contain. Therefore$list.index(del)will returnniland the call to#delete_atwill fail.What you need to do is
$list.delete_at(del).