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 1

kevinthecoder
kevinthecoder
10,791 Points

Unable to complete the empty? method Challenge

Alright, this one has me totally lost. I watched the video three times and checked my work and still can't figure it out? Can anyone help?? Thanks! I have attached my code.

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 empty?(name)
    index = 0
    found = false
    todo_items.each do |todo_item|
      if todo_item.name == name
        found = true
      end
      if found
        break
      else
        index += 1
      end
    end
    if found
      return index
    else
      return false
    end
  end

end

3 Answers

Hi Kevin,

In the TodoList class, fill in the empty? method to return a boolean value. The empty? method should return true if there are no elements in the @todo_items array. The method should return false if there is more than one element in the @todo_items array.

Here's alternative way, Ruby doesn't require to use implicit return, we can look @todo_items = [] under initialize, that's empty array, right?

Best way is to use empty? method, that's what the question asked.

def empty?
  todo_items.empty?   # this method will return true if array is empty
end

It will return true due to . Hope that helps.

kevinthecoder
kevinthecoder
10,791 Points

Ouch, I wasn't even close. The early stuff was easy to learn but this Boolean stuff is almost over my head. My brain is starting to hurt (hahahaha).

Thanks, Salman. I couldn't figure out the next one either and posted a new question to the forum; perhaps you could advise on that one too if you're still online...

Yeah, it is a bit deceiving and confusing easily. Keep practice to get more grasps. For your another question, I fix that solution, you did great job except end missing. :smile:

Ben Wong
Ben Wong
19,426 Points

if todo_items.empty? return true else return false