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 trialsowmya sunkari
Courses Plus Student 1,764 PointsThe 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>
Martin Cornejo Saavedra
18,132 PointsAnd the code?
sowmya sunkari
Courses Plus Student 1,764 Pointssame 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
5,689 PointsHi,
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!
sowmya sunkari
Courses Plus Student 1,764 Pointssowmya sunkari
Courses Plus Student 1,764 PointsWow, the memory addresses were printed in my output and when I pasted that in the question, but are not visible here. Strange