Why can't I call the create method like this?


Why is this wrong to put my_computer = Computer.create(“text”) at the end?
I’d like to just print the text!

undefined method `create’ for Context::computer:Class
but… the method create is there?

class Computer

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

  def Computer.get_users
    return @@users

my_computer = Computer.create("text.txt")
puts my_computer


create is is a instance method, so you would first need an instance and then call the method:

my_computer = Computer.new('username', 'password')

my_computer is the instance, now we can call the method

or you would have to convert the method to a class method, so you can call the method on class (Computer), see example:


however, create becoming a class method is problematic, how does it know #{@username} then?


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