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

challenge build a simple todo list program 12/12

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.

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)

  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

6 Answers

It seemed like the obvious answer was to use the existing function

def contains?(name)
  find_index(name) >= 0
end

But that didn't work.

Neither did the first answer using the array method "include?"

So I tried stealing the find_index code that passed

def contains?(name)
  index = 0
  found = false
  todo_items.each do |item|
    found = true if item.name == name
    break if found
    index += 1
  end
  found
end

Sorry about that, Gaylen, I didn't pay attention to the fact that the add_item instance method is adding a new object each time it gets called.

Yeah, your solution is correct, you can write it more concisely like this

def contains?(name)
  @todo_items.each {|item| return true if item.name==name}
  false
end

could be a stupid question but just wondering... I know your if statement doesn't require an end because it is on one line but i'm assuming your .each statement doesn't either because you used the curly brackets instead of do and end, am i correct?

def contains?(name)
  @todo_items.include?(name)
end

That'd be my answer based on the code your provided.

This was my solution too (minus the @ sign since there is an attr_reader) but it keeps saying contains? does not return a boolean. I ran it through irb though and it does >.<

Struggled with the same code, it just wouldn't work. I think there is too much copying in the challenges. Actually we had to just copy and paste the same code we wrote in the previous video. It's not actually challenging :)

It was mine, but the test failed. I'm not sure if there's a nil value being passed somewhere. Thanks for your one line posted up-thread.

you can also do this:

def contains?(name)
  !find_index(name).nil?
end

If you follow along the course they are referencing what they did in the remove_item method. If you write your code without completing the delete_at process it passes the contains? requirement.

def contains?(name)
    if index = find_index(name)
      return true
    else
      return false
    end
  end

def contains?(name) if todo_items = find_index(name) return true else return false end end