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

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: