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

Caleb Kleveter
MOD
Caleb Kleveter
Treehouse Moderator 37,862 Points

Returning boolean values: part 2.

The instructions for this challenge are as follows: "In the TodoList class, fill in the contains? method so that it returns a boolean value if the todo items array contains an item with the name argument."

With my code I get a bummer message that says try again!

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)
    todo_items.each do |todo_arguments|
      return todo_arguments.try_convert(name)
      end
  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

If I try this code:

 if index = find_index(name)
      return true
    end

I get this message: " Bummer! The contains? method did not return a boolean value."

3 Answers

Hi Caleb,

So close I revise your code example, here's alternative way. It is much easier to read and understand easily :wink:

  def contains?(name)
    todo_items.each { |todo_arguments| return true if todo_arguments.name == name }
    false
  end
Stephen Printup
seal-mask
.a{fill-rule:evenodd;}techdegree
Stephen Printup
UX Design Techdegree Student 45,252 Points

I struggled with the same question and used this:

  def contains?(name)
    todo_items.each do |todo_item|
      if todo_item.name == name
        return true
      else
        return false
      end
    end
  end

I think it's a bit closer to what the videos were teaching, but whatever works.

Caleb Kleveter
MOD
Caleb Kleveter
Treehouse Moderator 37,862 Points

Thanks! Did I mis something in a video, because I don't recognize that code.

The logic is from the find_index method :smile: