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

sowmya sunkari
PLUS
sowmya sunkari
Courses Plus Student 1,764 Points

The memory address of the instance todo_item is still printed to the screen even after defining the to_s method. Help?

This is the output after printing the todo_item.

treehouse:~/workspace$ ruby todo_list.rb
[I] milk

<TodoItem:0x0055f943842208>

[C] milk

<TodoItem:0x0055f943842208>

[I] milk

<TodoItem:0x0055f943842208>

sowmya sunkari
sowmya sunkari
Courses Plus Student 1,764 Points

Wow, the memory addresses were printed in my output and when I pasted that in the question, but are not visible here. Strange

sowmya sunkari
sowmya sunkari
Courses Plus Student 1,764 Points

same as in the video.

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

class TodoItem
attr_reader :name

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

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

  def complete?
    @complete
  end

  def mark_complete!
    @complete = true
  end

  def mark_incomplete!
    @complete = false
  end
end

1 Answer

Jamaal Todd
Jamaal Todd
5,689 Points

Hi,

Not sure if you have solved this yet. But your problem is that in your "to_s" method. Your calling the "puts" method, which is not needed. Because the "to_s" method will automatically print out the string when the "to_s" method is called.

So you can remove "puts" from both lines which you have used them for the "to_s" method.

Good Luck!