'Ruby: undefined method `<<' for nil:NilClass error message

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!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source