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 Part 2: Todo Item Methods

Sean Flanagan
Sean Flanagan
33,235 Points

What's wrong with my code?

Hi. I've got a problem with this.

todo_list.rb:

require "./todo_item"

class TodoList
  attr_reader :name, :todo_items
  def initialize(name)
    @name = name
    @todo_items = []
  end
end

todo_list = TodoList.new("Groceries")
todo_item = TodoItem.new("Milk")

puts todo_item
todo_item.mark_complete!
puts todo_item
todo_item.mark_incomplete!
puts todo_item

todo_item.rb:

class TodoItem
  attr_reader :name

  def initialize(name)
    @name = name
    @complete = false
  end

  def to_s
    if complete?
      "[C] #{name}"
    else
      "[I] #{name}"
    end
  end

  def complete
    @complete
  end

  def mark_complete!
    @complete = true
  end

  def mark_incomplete!
    @complete = false
  end
end

Error:

/home/treehouse/workspace/todo_item.rb:10:in `to_s': undefined method `complete?' for #<TodoItem:
0x0056255de57940 @name="Milk", @complete=false> (NoMethodError)                                  
        from todo_list.rb:14:in `puts'                                                           
        from todo_list.rb:14:in `puts'                                                           
        from todo_list.rb:14:in `<main>'  

Does anyone know why this won't run please? Thanks. :-)

2 Answers

John Steer-Fowler
PLUS
John Steer-Fowler
Courses Plus Student 11,734 Points

Hi Sean,

Try entering this into todo_item.rb instead of your current version:

def to_s
    if complete
      "[C] #{name}"
    else
      "[I] #{name}"
    end
end

Hope this fixes it

Sean Flanagan
Sean Flanagan
33,235 Points

Hi John. I deleted the question mark and this is what I got:

[I] Milk                                                                                         
[C] Milk                                                                                         
[I] Milk  

Thanks for your help. :-)

John Steer-Fowler
John Steer-Fowler
Courses Plus Student 11,734 Points

No problem.

It's fine to use question marks in method names, but you need to define the method with the question mark in it's name first.

Note that some built-in Ruby methods have question marks, but this is different to defining your own methods.

Glad I could help

Oğulcan Girginc
Oğulcan Girginc
24,848 Points

In the todo_item.rb file, the if statement (10th line) calls the complete? method. However, your method is named complete (which is on 17th line) and doesn't have the question mark.

You can either delete the question mark as it was suggested or can add the question mark to your if statement.