Hi there,
I’m having trouble understanding how to work with class variables across different files.
I have a parent class defined in file ‘game.rb’ which creates a variable ‘score’.
I have a sub class defined in file ‘TestGame.rb’ which requires the file ‘game.rb’
I have a file ‘start.rb’ that is creating an instance of sub class, and I need to access the variable ‘score’ from its parent class.
I feel like I’ve tried everything I can think of, and am coming up way short.
File ‘game.rb’ has the following code:
class Game
attr_accessor(:name, :score)
def initialize(name)
@name = name
@score = nil
end
def win
puts "You win!"
score = 1
end
end
File ‘TestGame.rb’ has the following code:
require_relative 'game'
class TestGame < Game
def initialize
end
def play()
puts "This is my test game."
win
end
end
File ‘start.rb’ has the following code:
require_relative 'game'
require_relative 'TestGame'
TestGame.new.play
TestGame.score
I’m able to get the files to load correctly and play the instance of my game, but once the game is over, I’m wanting to access the score, and that’s the piece that I can’t get to work.
What am I not understanding correctly or missing?