FAQ: Virtual Computer - You Did It!

This community-built FAQ covers the “You Did It!” 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 You Did It!

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!

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 "The file {@files[filename]} was created at {@time}"
end

Could you have created def create(filename)
with time = Time.now ?

I am wondering if time has to be made an instance variable or can it be a variable for the create method only?

The instance variable is necessary in order to hold on to the current time value for this file. If we use the class directly it will be different in each time.

Okay thank you. I also don’t know why in that solution it gave the puts statement the way it did,

@files[filename] just gives the time, and there are no #

when – “The file #{filename} was created at #{@time} by #{@username}” works and that should be the correct syntax?

Yeah, I thought that looked odd but opted to not mention it figuring you would spot it.

Ruby Programming/Syntax/Literals - Wikibooks, open books for an open world

As to what the author is attempting to convey, that is not for me to say. Once you get a pass on the lesson, stay with it and play around with the variables you interpolate.and study the output.

I did give your earlier question some more thought. What if we did something like this?

    @files[filename] = Time.now
    puts "The file #{filename} was created at #{files[filename]} by #{@username}."

Then we don’t need the instance variable, at all.

1 Like

I have another question :laughing:

def self.display_files
@@files
end

puts “Files: #{Computer.display_files}”

VS

def Computer.display_files
@files
end

Computer.display_files

Is there a time or place where self.method is better than Classname.method ?

When privacy is important, then class variables are valuable since they can only be polled by class methods, and not from the outside. An instance gains access through its own method that can return the class method as a callable object.

Not sure I’ve seen a signature line like this, before. Might take some extra study to suss out whether or not it is valid and correct.

I took it directly from the example code given!

class Computer
$manufacturer = “Mango Computer, Inc.”
@@files = {hello: “Hello, world!”}

def initialize(username, password)
@username = username
@password = password
end

def current_user
@username
end

def self.display_files
@@files
end
end

Make a new Computer instance:

hal = Computer.new(“Dave”, 12345)

puts “Current user: #{hal.current_user}”

@username belongs to the hal instance.

puts “Manufacturer: #{$manufacturer}”

$manufacturer is global! We can get it directly.

puts “Files: #{Computer.display_files}”

@@files belongs to the Computer class.

1 Like

My bad. Parameterless methods can be used as getters. self in this context is the class, not the instance.

Hello ! I’m working on a method to update files. So far My method does update the filename but I would like to refer back to the previous filename in what gets outputted by puts. However, I don’t think I’m saving it anywhere. Any help would be greatly appreciated ! Thank you

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

def Computer.get_users
@@users
end
end

my_computer = Computer.new(“jim”, 01234)
my_computer.create(“booky wookie”)
my_computer.update(“looky cookie”)
puts “#{Computer.get_users}”

output:

The file booky wookie was created by jim at 2019-10-07 15:53:52 +0000.
The file looky cookie was updated by jim at 2019-10-07 15:53:52 +0000.
{“jim”=>668}

I’ve been trying to implement an update/rename method. Not sure if this is mean to go in the class Computer? it shows up on the screen alright but I cannot get user input from the terminal.

any suggestions?

Thanks in advance.

class Computer
  @@users = {}

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

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

  def delete(filename)
    time = Time.now
    if @files.include?(filename)
      @files.delete(filename)
      puts "#{filename} deleted by #{@username} at #{time}."
    else
      puts "#{filename} not found."
    end
  end

  def Computer.get_users
  return @@users
  end

  def get_files
  return @files
  end

def update()
  puts "What file would you like to update?"
  @files.each_key { |key| print key + " " }
  filename = gets.chomp
  puts "What would you like to rename #{filename} to?"
  new_name = gets.chomp
end

end

my_computer = Computer.new("chris", "12345")
your_computer = Computer.new("eric", "abcd")

my_computer.create("hello.txt")
my_computer.create("groceries.txt")
your_computer.create("todo.txt")
my_computer.create("files.txt")
my_computer.delete("files.txt")

my_computer.update()


The exercise mentions

def name=(value)
  @name = value
end

but we haven’t seen this in the previous exercises at all and that is why I have no context and don’t understand what this is doing at all

Where did you see that code? Please supply a link to the exercise. Thanks.

The above code is a Ruby setter method akin to,

 attr_writer :name

which should be coming up soon, if it hasn’t already.

woops my bad
I posted in the wrong thread.
I meant for this exercise https://www.codecademy.com/courses/learn-ruby/lessons/object-oriented-programming-ii/exercises/attrreader-attrwriter
and to post in this thread
FAQ: Object-Oriented Programming II - attr_reader, attr_writer
but I still have the same question. That syntax doesn’t look familiar to before that exercise and I’m very confused in what the setter method does

It does what the name says… Sets the variable with a new value.

I see.
So referring back to this original post and exercise.
https://www.codecademy.com/courses/learn-ruby/lessons/virtual-computer/exercises/you-did-it-1
I had this bit of code which passed the exercise. But there was no setter since I had not learned it yet.
Or is something else in place of it?

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

@@users = {}

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

def Computer.get_users
return @@users
end
end

my_computer = Computer.new("john", "password")

Nothing in place of it. There is no setter in the above class, nor any need of one, the way it stands.

1 Like

Hi everyone!

I’ve reached the end of the exercise, but my code doesn’t puts anything to the console. Is there something else I need to do? I’m at the end of the lesson, but there is no mention of adding any other code?

Thanks

Your code would not puts anything to the console unless you were to use my_computer.create, for example my_computer.create("myfile.txt")

As a side note, the string interpolation provided in the Codecademy solution looks odd:
puts "The file {@files[filename]} was created at {@time}"

seemingly should be:

puts "The file #{filename} was created at #{@time}"