Final Project. Handling Task object as a string


#1

Hello,

I'm David and I've been using different areas of Codecademy for years. I love it! I'm stuck on a part of the final project. When I show my items, they come out as something like this when I type 2 for show tasks:

I think I'm not hanging the Task object correctly as a string based on the clues in the text but when I copy the word for word solution, mine is identical. Any advice would be appreciated. Thank you!

`
module Menu
def menu
" Welcome to the TodoList Program!
This menu will help you use the Task List System
1) Add
2) Show
Q) Quit "
end
def show
menu
end

end

module Promptable
def prompt(message = 'What would you like to do?', symbol = ':> ')
print message
print symbol
gets.chomp
end
end

class List
attr_reader :all_tasks
def initialize
@all_tasks = [ ]
end

def add(task)
	all_tasks << task
end
def show
	all_tasks
end

end

class Task
attr_reader :description
def initialize(description)
@description = description
end
end

if "todo.rb" == $PROGRAM_NAME
include Menu
include Promptable

my_list = List.new
puts 'Please choose from the following list'
until ['q'].include?(user_input = prompt(show).downcase)
	case user_input
		when '1'
			my_list.add(Task.new(prompt('What is the task you would like to accomplish?')))
		when '2'
			puts my_list.show
		else
			puts "Sorry, I did not understand"
	end
end
puts 'Outro - Thanks for using the menu system!'

end`


#2

I'm not entirely sure what it is you're asking


#3

Strange. One of the lines didn't come out. The response I get when outputting my object as a string is

#<Task:0x007faaa28dccb0>

#4

So.. you're asking how to get puts to display the description of a Task.
You would need to implement that behaviour, otherwise Ruby won't know what to print
This is done by giving your class a method with the name to_s, the string returned is what array and puts will use:

class Thing
  def to_s
    'meow'
  end
end

puts [Thing.new, Thing.new]

meow
meow

As an alternative, or both, you could do this for the class whose instances represent lists of tasks


#5

So, is this "to_s" method built in? Like the "initialize" method? It's the first time I've seen it used in a class. I'm used to seeing methods defined so they can be called later, but this one appears to automatically execute.

Also, how do we know when to use this? Is it because "attr_reader" converts the attribute to a symbol?