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

What is wrong with my answer?

I dont understand why this is wrong.

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?
    if(@todo_items.length == 0)
      return true
    else
      return false
  end

end

3 Answers

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

You're missing one end after the return false line.

thanks I don't know why I didn't notice that.

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

though not related, I'd like point out that you can shorten your empty? method like this:

def empty?
  @todo_items.length==0
end

And it behaves exactly the same.

"The method should return false if there is more than one element in the @todo_items array."

I'm confused on this one. My code included a conditional to return false ONLY if there was MORE THAN ONE item in the array (meaning greater than 1)

def empty?
    if @todo_items.empty?
     return true
   end 
    if @todo_items.length > 1
     return false
    end 
  end

but the code that passes is

def empty? @todo_items.empty? end

should the question say "at least one element"? I don't see this asked elsewhere - what am I misunderstanding? Thanks everyone!

William Li
William Li
Courses Plus Student 26,868 Points

should the question say "at least one element"? I don't see this asked elsewhere - what am I misunderstanding?

No, you are not misunderstanding, and I agree that "at least one element" is a much better description for what this challenge is asking for.