Getting More Creative- scope


#1

how can the create method reach variable @files which is in the initialize method?

https://www.codecademy.com/courses/learn-ruby/lessons/virtual-computer/exercises/getting-more-creative?link_content_target=interstitial_lesson

my code is passing the exercise



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 "file is created"
  end
end


#2
  def getfiles
    @files.each {|x| puts x}
  end
my_computer.getfiles
["new_file.txt", 2017-11-05 18:59:50 +0000]
["next_file.txt", 2017-11-05 18:59:50 +0000]
["last_file.txt", 2017-11-05 18:59:50 +0000]

#3

Hi, thanks for your response.
I don’t get how that answers my question.

My code is working fine, I just wanted to know how does create method reach variable @files which belongs to initialize method?


#4

Have you tried using an accessor?


#5

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