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 () 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 () below!
Agree with a comment or answer? Like () to up-vote the contribution!
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.
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}."
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.
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
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()
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?