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

Sarah A. Morrigan
Sarah A. Morrigan
14,329 Points

why is this syntax so asinine?

def contains?(name)
   if !(todo_items.name.nil?)
      return true
    end
  end

I am not learning anything and I no longer have an ability to continue. these code challenges are absolutely incomprehensible.

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)
    if !(todo_items.name.nil?)
      return true
    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
Brandon Keene
Brandon Keene
7,217 Points

No, you can do it! I felt the same way, but it opened up for me, and I bet it'll open up for you too.

Try this:

  def contains?(name)
    todo_items.any? {|item| item.name==name}
  end
Sarah A. Morrigan
Sarah A. Morrigan
14,329 Points

I was not expecting any response.

.any? I have never seen this method before.

Daniel Cunningham
Daniel Cunningham
21,109 Points

The challenge uses the same syntax that one of the earlier videos demonstrated (although they called the method by a different name).

They want you to use the "find_index" method to produce a true or false statement. Find index will only produce a value if a string can be found within the todo list, otherwise, the value is nil.

my syntax was: <blockquote> <p>def contains? (name)</p>

<p> if index = find_index(name)</p>

<p> return true</p>

<p> else</p>

<p> return false</p>

<p> end</p>

<p>end</p>

</blockquote> (pardon the poor spacing, I havent figured out the markdown language yet)

I'm sure there are much shorter ways to find the item, but for the purposes of the assignment, that gets the job done using the methods we learned.

Hope that helps!