Skip to content

Instantly share code, notes, and snippets.

@sirupsen

sirupsen/tdo.rb Secret

Created December 12, 2009 23:16
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save sirupsen/6656de5cc00df7802c5e to your computer and use it in GitHub Desktop.
Save sirupsen/6656de5cc00df7802c5e to your computer and use it in GitHub Desktop.
Gist for "What I Wish a Ruby Programmer Had Told Me One Year Ago".
class TodoList
def self.load(file)
# read the file, create a list, create items, add them to the list, return the list
list = TodoList.new
File.read(file).each_line do |line|
list.add TodoItem.new(line.rstrip)
end
list
end
def initialize
@list = []
end
def add(item)
@list << item
end
def write(file)
# write the file, only write the undone items
File.open(file, 'w') do |f|
f.write(@list.reject {|item| item.done?}.join("\n"))
end
end
def [](id)
@list[id]
end
end
class TodoItem
# provide reader and setter for name and state
attr_accessor :name, :done
alias_method :done?, :done
def initialize(name)
# store name
# set state to undone
@name = name
@done = false
end
end
# ---
# the library will be used like this:
# list = TodoList.load("todo.td")
# list[0].done = true
# list.add TodoItem.new("another cool item")
# list.write("todo.td")
#
list = TodoList.load("todo.td")
list[0].done = true
list.add TodoItem.new("Grow long hair for pro multitasking capabilities")
list.write("todo.td")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment