Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby Ruby Booleans Build a Simple Todo List Program Returning Boolean Values: Part 2

Include? method works in Workspaces but not in Challenge

I used the include method to return whether or not the name variable is in the @todo_items array. It works when I call it in workspaces. It does not work in the challenge. Why is this? The two lines I wrote are below:

var = @todo_items.include?(name)

return var

I had a similar problem on the previous challenge and I don't understand why neither worked. I want to make there's not something I'm misunderstanding.

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 contains?(name)
    var = @todo_items.include?(name)
    return var
  end

  def find_index(name)
    index = 0
    found = false
        todo_items.each do |item|
      found = true if item.name == name
      break if found
      index += 1
    end
    if found
      return index
    else
      return nil
    end
  end
end

1 Answer

Maciej Czuchnowski
Maciej Czuchnowski
36,440 Points

You have to take a closer look at the available code. You may want to use the find_index method. It returns either a number (if that item is found - numbers are "truthy" values) or nil (which is a "falsy" value) if it can't be found. Based on that you can return true or false from the method.