7. Instantiation Nation


#1

Try to print to the console, this is the result

VODO.txt was created by Moana at 2017-08-03 14:35:42 +0000
uninitialized class variable @@users in Context

Could anyone help me?

 class Computer
  @@users = {}
  
  def initialize(username, password)
    @username = username
    @password = password
    @@users[username] = password
    @files = {}
  end
  
  def create(filename)
    time = Time.now
    @files[filename] = time
    puts "#{filename} was created by #{@username} at #{time}"
  end
end

def Computer.get_users
    @@users
  end

my_computer = Computer.new("Moana", 000001)

my_computer.create("VODO.txt")
puts "User: #{Computer.get_users}"



#2

You ended your class after you defined the create method.

  def create(filename)
    time = Time.now
    @files[filename] = time
    puts "#{filename} was created by #{@username} at #{time}"
  end
end

See the extra end? That finished your class, Computer will never see the .get_users method.
Move that end after your final method and you’re all set


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.