Undefined method '<<' Error


#1

Just started the final project. When I try to push the task into the all_tasks array I get the following error:
todo.rb:7:inadd_task’: undefined method <<' for nil:NilClass (NoMethodError)

class List
  attr_reader :all_tasks
  def intialize
    @all_tasks = []
  end
  def add_task(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 new list'
  my_list.add_task(Task.new('Make Breakfast'))
  puts 'You have added a task to the Todo List'
end

What exactly am I doing wrong? I am using cloud 9 to write all my code.


#2

Please remember to include a link to the exercise when posting a question in Q&A. Thanks.

Does that look right to you?


We can trace this error message back to that typo, since as a result the array @all_tasks does not exist, so cannot have a push method. That is what nil:NilClass means. nil does not have any methods or attributes.


#3

Thanks. I need to rest, apparently my eyes aren’t working.


#4

Thanks a bunch! I’ll have to be more careful. At least it was a learning experience!


#5

That’s great! You’re welcome. Expect lots more of little trip-ups along your way. It happens, and the more it does, the more chance we have to sharpen up on the little details AND muscle memory, troubleshooting memory, etc. Mistakes are our friends.