Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Ruby Booleans Build a Simple Todo List Program Finding Array Items

Ruby Booleans todo list challenge

First workspace is not working. Secondly I don't understand the question

todo_list.rb
class TodoList
  attr_reader :name, :todo_items

  def initialize(name)
    @name = name
    @todo_items = []
  end

  def add_item(name)
    todo_items.push(TodoItem.new(name))
  end

  def find_index(name)
    index = 0
    found = false
  end

end

1 Answer

Tom Sager
Tom Sager
18,987 Points

<p>First, I have the same problem with the workspace. It works on other lessons, but not on this one.</p> <p>I am not sure what you don't understand, but let me try to describe things to you.</p> <p>A TodoList instance has a name and an array of TodoItems. So if we create a new list like this:

mytodolist = TodoList.new('my list')

We can get its contents:

mytodolist.name()        # returns 'my list'
mytodolist.todo_items()  # returns [] - an empty array

Then we can add a couple of todo items to this list:

mytodolist.add_item("make breakfast")
mytodolist.add_item("brush my teeth")

Now the items in the list are:

mytodolist.todo_items()  # returns an array with two items:
    [ "make breakfast", false ]
    [ "brush my teeth", false ]

<p>The list of items has already been created for the challenge. The answer involves iterating over each of the items to see if its name matches the given string (hint: 'each'). Because find_item is an instance variable, mytodolist.find_item 'automatically' references the correct array of TodoItems.</p>

<p>Does this help make it clear?</p>

<p>Note that the answer in the lesson uses 'each' with an index that is initiated outside the loop. An additional method 'with_index' makes the code a lot cleaner:</p>

todo_items.each.with_index do |item, index|
    ...
end