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 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,441 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.