Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding code/todo_list.rb #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions code/todo_list.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
class TodoItem
attr_accessor :description
def initialize(description, done=false)
@description = description
@done = done
end
def done?
@done
end
def done!
@done = true
end
end

class TodoList
attr_reader :items, :color, :name
def initialize(name, opts = {})
@name = name
@color = opts[:color]
@items = []
end
def add(item)
if item.is_a? String
@items << TodoItem.new(item)
else
@items << item
end
end

def items_pending
items.select {|e| not e.done?}
end
def items_done
items.select {|e| e.done?}
end
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

apropo:

[1] pry(main)> (1..10).to_a.select(&:odd?)
=> [1, 3, 5, 7, 9]
[2] pry(main)> (1..10).to_a.reject(&:odd?)
=> [2, 4, 6, 8, 10]

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super tare! Exista functie pentru aproape orice 😆. Si devine mai frumos items.reject(&:done?) si items.select(&:done?)

def find_by_description(description)
items.find {|e| e.description == description}
end
def set_as_done(description)
item = self.find_by_description(description)
if item
item.done!
else
false
end
end

end