Let's put variables, input, arrays, hashes, methods, and loops together to build a small command-line Task Tracker. It's a tiny project, but it's a real taste of the logic behind actual apps.
ruby
tasks = []
def show_menu
puts "
Task Tracker"
puts "1. Add task"
puts "2. Show tasks"
puts "3. Exit"
print "Choose: "
end
loop do
show_menu
choice = gets.chomp
case choice
when "1"
print "Task title: "
title = gets.chomp
tasks << { title: title, done: false }
puts "Task added."
when "2"
if tasks.empty?
puts "No tasks yet."
else
tasks.each_with_index do |task, index|
status = task[:done] ? "Done" : "Pending"
puts "#{index + 1}. [#{status}] #{task[:title]}"
end
end
when "3"
puts "Goodbye!"
break
else
puts "Please choose 1, 2, or 3."
end
endThis project stores each item as a hash inside the tasks array. The menu is split out into its own method, and loop do keeps the program running. Whatever the user picks gets checked with case, which handles adding a task, showing the task list, and exiting.
You should see
Task Tracker 1. Add task 2. Show tasks 3. Exit Choose: 1 Task title: Learn Ruby Task added.Info
Combining arrays and hashes like this is great practice for understanding real app data structures.
Summary
Want to take it further? Try adding an option to mark a task done, a delete option, or a file save/load feature.