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 1

Empty? method (returning Boolean values)

Okay, so I've gotten things up to this point as the syntax is fairly straightforward, but for some reason I keep getting a "bummer, try again" on this exercise. Am I missing something obvious?

Also wouldn't I get nil if there is only one item in the todo_items array?

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)
    if @todo_items == 0
      return true
    else @todo_items >= 1
      return false
    end
  end

end

2 Answers

Hi Katriel,

I just used a straight comparison with zero. However, I don't think this fits the challenge but the code does pass.

def empty?
  @todo_items.count == 0
end

The question isn't covering the scenario where there is one item in the array, as you say! However, the code is expecting only two outcomes; true or false, based on empty & not empty. The challenge code doesn't distinguish three states, empty; has one; has more than one.

I hope that helps,

Steve.

In short, I agree with your interpretation of the question; your code reflects a solution to the question posed.

Oh, okay! Sometimes the questions are not always so straightforward in interpretation (because naturally I'd think you'd have to cover three cases - empty, more than one, one - based on the wording of the question) but the clarification helps in terms of the exercise. Thanks!

I agree this question isn't accurately defining what code is required to pass the challenge. But, it is the correct interpretation of a helper method like empty?. That should return only two possible values; true or false. Having other states is contradictory to the purpose of this type of method.