Virtual Computer: Who're the Users? ex. 6


#1

This is the link to the exercise:
https://www.codecademy.com/en/courses/ruby-beginner-en-X5wcR/0/6?curriculum_id=5059f8619189a5000201fbcb#

This is my code:
class Computer
@@users = {}
def initialize(username, password)
@username = username
@password = password
@files = {}
@@users[username] = password
def create(filename)
@filename = filename
time = Time.now
@files[filename] = time
end
def Computer.get_users
@@users
end
puts "Here is the info you are looking for, #{filename}: #{username} and #{time}"
end
end

Error message:
Oops, try again. Did you add a get_users method to your Computer class?

don't get it. It looks fine to me! can you please help?


#2

The above line looks like it belongs in the initialize method (but it doesn't). You can remove that line.

The end for initialize is in the wrong place. It should be above def create.

The class method could be written at the top, just inside the class.

Try these fixes and let us know if you are still having problems.


#3

Thank you for going over my code.

I tried the fixes.
There is this one, the last one you said to write the class method insdie the class the class at the top!
def Computer.get_users
@@users
end

Do you mean inside the Computer class method?

without trying that last fix I get this error message:
undefined local variable or method `time' for Context::computer:Class


#4

Inside the Computer class object, yes.

class Computer
    def Computer.get_users
        @@users
    end
    @@users = {}

    # ...

end

Did you do this fix?

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

And did you remove that line?

    def create(filename)
        time = Time.now
        @files[filename] = time
    end

#5

I did. but still had issues until I deleted the puts statement at the bottom of the code. Then it worked. Why was that?


#6

Must admit I left that one off the list on purpose so that when it came up as an issue we would know we are at the end of the problems. Line statements (essentially commands) cannot be written into a class object, only in a method.


#7

slight_smile: thanks a bunch! you have been a great help!


#8

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

This makes a little more sense, perhaps? Now the line is inside a method.


#9

Yes it does! that puts statement was added early on and as we added more components to the code it got shifted to the wrong place outside the class Computer.


#10

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