Part of my array comes out as an index location

Working on a to do list project. Could someone tell me why the after I
add a task in case ‘1’ when I display the list using option ‘2’ it
prints out something like #Task:0x27eaff0?

Here is my code:

module Menu
def menu
“Welcome to the To Done list of the future!
Enter 1 to add a task
Enter 2 to show tasks
Enter Q to quit\n”
end

def show
menu
end
end

module Promtable
def prompt(message = “Well get to it then”, 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 :desc

def initialize(desc)
@desc = desc
end
end

if FILE == $PROGRAM_NAME
include Menu
include Promtable
my_list = List.new
until [‘q’].include?(user_input = prompt(show).downcase)
case user_input
when ‘1’
my_list.add(Task.new(prompt(“What do you want to add?”)))
when ‘2’ then puts my_list.show
else puts “That doesn’t work here buddy\n”
end
end
puts “Peace Out”
end

If you look in the documentation:
http://ruby-doc.org/core-2.2.2/IO.html#method-i-puts

You get redirected to the documentation for IO#print:
http://ruby-doc.org/core-2.2.2/IO.html#method-i-print

Which says:
“… Objects that aren’t strings will be converted by calling their to_s
method.”

So, what you need to do, is override the Object#to_s method, from which
your Task and List classes are derived.

So, for example,

class Task
def to_s
“#{self.desc}”
end
end

Great, thank you!! I spent a decent amount of time trying to figure it
out.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs