Got error message "undefined method `[]=' for nil:NilClass"


#1

Hi guys,

I'm getting error message "undefined method `[]=' for nil:NilClass". Can someone help to solve? Thanks.

My code:

class Computer
@@users = {}
def initialize (username, password)
@username = username
@password = password
@files = {}
@@users[username] = password
end

def create (filename)
    time = Time.now
    @file[filename] = time
    puts "#{filename} has created successfully at #{time}"
end

def Computer.get_users
    return @@users
end

end

my_computer = Computer.new("acer", 123456)
my_computer.create("ruby.txt")


#2

I fixed it removing @files and @@users form initialize method. Then it went on. Honestly I ignore the reason. Maybe it is a bug


#3

First, but don't you have @files = {} in the initialization and you are calling @file[filename] (Without the s) later. That would seem like a reason for the error as well...