Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Any idea what the issue is?

Thanks!

>Solution :

@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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading