Concepts Used: Arrays, Loops, Methods, File Handling
tasks = []
loop do
puts "\n--- To-Do List ---"
puts "1. Add Task"
puts "2. View Tasks"
puts "3. Remove Task"
puts "4. Exit"
print "Choose option: "
choice = gets.chomp.to_i
case choice
when 1
print "Enter task: "
task = gets.chomp
tasks << task
puts "Task added!"
when 2
puts "\nYour Tasks:"
tasks.each_with_index { |t, i| puts "#{i+1}. #{t}" }
when 3
print "Enter task number to remove: "
num = gets.chomp.to_i
if num > 0 && num <= tasks.length
tasks.delete_at(num-1)
puts "Task removed!"
else
puts "Invalid task number."
end
when 4
puts "Goodbye!"
break
else
puts "Invalid choice!"
end
end
Explanation:
- Stores tasks in an array.
- Uses
loop do ... end
for continuous menu. each_with_index
→ shows tasks with numbers.delete_at
→ removes task by index.
Leave a Reply