Skip to content

Instantly share code, notes, and snippets.

@kmandreza
Created June 26, 2012 00:33
Show Gist options
  • Save kmandreza/2992328 to your computer and use it in GitHub Desktop.
Save kmandreza/2992328 to your computer and use it in GitHub Desktop.
Re-refactored ToDo
class List
def initialize(title)
@list = []
@title = title
end
def append(item)
if item.is_a?(Item)
@list << item
else
return "not a valid item"
end
end
def prepend(item)
@list.unshift(item)
end
def add_a_bunch(array_of_items)
array_of_items.each do |item|
@list << Item.new(item)
end
end
def delete(item_description)
@list.each do |item|
if item.description == item_description
@list.delete(item)
end
end
end
def delete_at(number)
@list.delete_at(number.to_i - 1)
end
def completed_items
@list.select { |item| item.complete? }
end
def complete_an_item(description)
@list.each do |item|
if item.description == description
item.complete
end
end
end
def write_file
File.open("marykharatodo.txt", "w").puts(@list.inspect)
end
end
class Item
attr_reader :description, :completed, :creation_time
def initialize(description)
@description = description
@completed = nil
@creation_time = Time.now
end
def complete?
@completed != nil
end
def complete
@completed = Time.now
end
end
list = List.new("grocery")
puts list.inspect
list.add_a_bunch(%w"greens fruits junk fruits mangoes bananas papayas kiwi")
puts list.inspect
list.delete_at(1)
puts list.inspect
list.complete_an_item("kiwi")
puts list.inspect
list.completed_items
list.write_file
puts list.inspect
# list.delete("fruits")
# puts list.inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment