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.

Katriel Paige
9,260 PointsEmpty? 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?
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

Steve Hunter
57,696 PointsHi 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.

Katriel Paige
9,260 PointsOh, 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!

Steve Hunter
57,696 PointsI 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.
Steve Hunter
57,696 PointsSteve Hunter
57,696 PointsIn short, I agree with your interpretation of the question; your code reflects a solution to the question posed.