Final Project - - Now Solved - - How did it get fixed?


#1

Hello, first time posting on forums!

I've gotten myself stuck on my final project. I am trying to make it create a new task.

the result reads when I run it in terminal.

test.rb:9:in 'add': undefined local variable or methad 'all_tasks' for #<List:0x29fa798 @all_tasks=[]

This is the code I'm currently using.

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

	def add(task)
		all_tasks << task
	end

	def show
		all_tasks
	end


#Things left to do

#Insert list

#Add task to list

#Show all tasks

#Read a task from a file

#Write a list to a file

#Delete a task

#Update a task

end


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

#Runs the program

if __FILE__ == $PROGRAM_NAME
	my_list=List.new
	puts 'you have created a new list'
	my_list.add(Task.new('THIS is a DESCRIPTION'))
	puts 'you have made a new task'
end

My objective is to make the error go away so I can continue coding. As it stands, it's somehow broken because I probably messed up the format somehow. Could someone give me a hint to where it is?

Command says line 9... I've been staring at it for a couple hours looking through web pages to find hints


#2

I believe I fixed it.

I came back to it after a short while. Realizing it wasn't a lot of stuff, I just rewrote it all.

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

	def add(task)
		all_tasks << task
	end
end

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

if __FILE__==$PROGRAM_NAME
	my_list = List.new
	puts 'you have created a list'
	my_list.add(Task.new('Make breakfast?'))
	puts 'Now we have a new task in the todo list!'
end

It prints out both items now.

#3

did you mean, @all_tasks?


#4

Everything prints now, and I've carried on with the final and have been making progress :slight_smile:

I still admittedly do not know where my mistake lies...

@all_tasks? Did I fail to make something global?


#5

all_tasks is not defined, but the instance variable, @all_tasks is, within the class.


#6

Ah, thank you! :sweat_smile:

I should have added an all_tasks definition within my class definitions.

It sure wouldn't work without it and that explains the prompt.:expressionless:

Edit: I have completed my final and I'm happy with my result. From here, I will go to HTML and work my way down the proper way. From there, I will come back to Ruby and maybe do the exercises and final again without using the hints as much and to jog my memory.