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

How do I return a boolean when an array is empty?

This is the question I'm currently working with:

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 is the code as it was:

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 = nil
        return true
      else
        return false
  end
end

and one of my solutions:

  def empty?
    if @todo_items = nil
        return true
      else
        return false
  end

I've tried "[]" "[""]" "0"

I've also tried: @todo_items.nil? @todo_items.any?

9 Answers

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

First, you can NOT check if an Array is empty by checking it against nil

[] == nil   # => false

This doesn't work, empty Array doesn't equal to nil

Now for you problem, you can make use of the Array build-in method empty? to do the job http://ruby-doc.org//core-2.2.0/Array.html#method-i-empty-3F

def empty?
  todo_items.empty?
end

alternatively, you can tell if Array is empty by checking its length

def empty?
  todo_items.length == 0
end
William McManus
William McManus
14,819 Points

That first bit was brilliant! Guess there was a hint in the Array name huh:)

Hunter Garrett
Hunter Garrett
6,339 Points

I wrote

def empty?
    if todo_items.length == 0
      return true
    elsif todo_items.length > 1
      return false
    end

I think your problem is that you're assigning instead of comparing with your conditional statements. "=" vs. "==" look at your if statements

Thank you both for your answers. I actually was missing an "end". :|

William, thank you for the additional information about nil.

You don't need to use the @ for the instance variable since todo_items is already defined in the attr_reader method.

Kasia Ekiert
PLUS
Kasia Ekiert
Courses Plus Student 13,248 Points

def empty? if @todo_items != [] empty = false else empty ||= true end end

I did it like this:

if @todo_items == [] return false else return true end

def empty?
    answer = false
    if todo_items.length == 0
      answer = true
    end
    return answer
 end
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?
    @todo_items == [] ? true : false
  end

end