Skip to content

Instantly share code, notes, and snippets.

@ndemonner
Created March 8, 2012 05:06
Show Gist options
  • Save ndemonner/1998832 to your computer and use it in GitHub Desktop.
Save ndemonner/1998832 to your computer and use it in GitHub Desktop.
Simple todo app for Fay
class TodoApp
attr_accessor :todos
def initialize
@todos = []
end
def start
puts "Hi there! Welcome to Just Do It!"
choose_mode
end
def choose_mode
puts "Would you like to create, list, or delete a todo?"
choice = gets.downcase.chomp
while not valid_choice(choice)
puts "Sorry, that isn't a valid choice. Please type either 'create' or 'delete' or 'list'."
choice = gets.downcase.chomp
end
if choice == "create"
create_mode
elsif choice == "delete"
delete_mode
else
list_mode
end
end
def create_mode
puts "What do you need to do?"
choice = gets.chomp
while choice != "stop"
add(choice)
print_todos
puts "Great, enter another or type 'stop' to finish creating todos"
choice = gets.chomp
end
choose_mode
end
def delete_mode
print_todos
puts "Which # would you like to delete? Or type 'stop' to go back to the main menu."
choice = gets.chomp
choose_mode if choice == 'stop'
choice = choice.to_i
if choice > 0 && choice <= @todos.length
@todos.delete_at(choice - 1)
puts "DELETED!"
print_todos
delete_mode
else
puts "Sorry, that isn't a valid todo number. Try again!"
delete_mode
end
end
def print_todos
puts "Stuff to do:"
puts "---------------------------------"
@todos.each_with_index do |todo, index|
puts "#{index + 1}. #{todo}"
end
puts "---------------------------------"
end
def list_mode
print_todos
choose_mode
end
def add(todo)
@todos << todo
end
def valid_choice(choice)
return true if ["create", "list", "delete"].include? choice
false
end
end
app = TodoApp.new
app.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment