I'm trying to load some data from a csv file into my program and get the following error message: undefined method `<<' for nil:NilClass error message
Here is my code:
def load_students
file = File.open("students.csv", "r")
file.readlines.each do | line |
name, cohort = line.chomp.split(",")
@students << ({:name => name, :cohort => cohort.to_sym})
end
file.close
end
@students
refers to an empty array.
Any idea what the issue is?
Thanks!
CodePudding user response:
@students
refers to an empty array.
Does it? (Apparently not, because the error message says it's nil
!!) You never showed us where this variable get initialised.
In ruby, all instance variables evaluate to nil
by default, if you don't define them as something else.