FAQ: Virtual Computer - Who are the Users?


#1

This community-built FAQ covers the “Who are the Users?” exercise from the lesson “Virtual Computer”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Ruby

FAQs on the exercise Who are the Users?

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!


#2

I think the users variable should not be a hash, since it cannot store users with identical names. So when storing two users with the same name the old one gets overwritten. See code below:

class Computer
  @@users = {}  # Hash, not a good choice
  
  def initialize(username, password)
    @username = username
    @password = password
    @files = {}
    @@users[username] = password
	end
  
  def Computer.leak_all_users_and_passwords
    @@users.each {|k,v| puts "User #{k} has password #{v}"}
  end
end

my_computer = Computer.new("thomas", 1234)
ben_computer = Computer.new("Ben", 13415)
thomas_computer = Computer.new("thomas", 247686)
Computer.leak_all_users_and_passwords
# where did the first user go?

EDIT: I fixed the problem by creating a new class “users”, and then creating an array of users as classvariable in computer class. Now each time a new instance of computer is generated also a new user is generated and pushed to the classarray which holds all user objects. Maybe someone finds that helpful or has any feedback/improvements?

class User
  def initialize(username, password)
    @username = username
    @password = password
  end
  def show_personal_data
    puts "username: #{@username}, password: #{@password}"
  end
end


class Computer
  @@users = []  # array instead of hash to prevent overriding duplicate users

  def initialize(username, password)
    @username = username
    @password = password
    @@users << User.new(username, password)
	end

  def Computer.leak_all_passwords
    @@users.each { |user| user.show_personal_data }
  end
end

my_computer = Computer.new("thomas", 1234)
his_computer = Computer.new("Ben", 13415)
another_thomas_computer = Computer.new("thomas", 247686)
Computer.leak_all_passwords  # now all three users are displayed!