Skip to content

Instantly share code, notes, and snippets.

@libryder
Created April 10, 2012 18:42
Show Gist options
  • Select an option

  • Save libryder/2353584 to your computer and use it in GitHub Desktop.

Select an option

Save libryder/2353584 to your computer and use it in GitHub Desktop.
class Item
attr_accessor :description, :completed
def initialize desc
@description = desc
@completed = false
end
end
class TodoList
def initialize
@list = [Item.new("Write this program.")]
end
def show_list
@list.reject do |item|
item.completed
end
end
def add_task desc=nil
if desc.nil?
"Must enter description"
else
@list.push Item.new desc
end
end
def mark_complete item
@list[item].completed = true
end
end
@list = TodoList.new
def print_list
@list.show_list.each_with_index do |item, index|
puts "#{index}. #{item.description}"
end
end
print_list
puts "Press 'a' + ENTER to add a ToDo"
puts "Press <Num> + ENTER to mark a Todo done"
puts "Press 'q' + ENTER to quit"
running = true
while running
input = gets
begin
input = Integer input
rescue
input = input.chomp!
end
case input
when "a"
puts "Enter description"
desc = gets
@list.add_task desc
print_list
when "q"
running = false
when 0..9999
@list.mark_complete input
print_list
end
end
require './todo_list'
describe 'list of todos' do
before (:each) do
@list = TodoList.new
end
it "returns the list" do
@list.show_list[0].should be_an_instance_of Item
end
it "add a task" do
@list.show_list.length.should == 1
@list.add_task("figure this out")
@list.show_list.length.should == 2
end
it "task will not be created without a description" do
@list.add_task().should == "Must enter description"
end
it "checks off the task" do
item = @list.show_list[0]
@list.mark_complete(0)
item.completed.should == true
end
it "does not return checked off task" do
@list.add_task("New task")
@list.mark_complete(1)
@list.show_list.length.should == 1
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment