class Player
def getsaves
print "Saves: "
saves = gets
end
def initialize(saves, era, holds, strikeouts, whip)
end
end
I have the code above…lets say I then write.
j = Player.new(30, 30, 30, 30, 30)
I want to access the saves variable in getsaves
When I am outside the class scope, how do I do this?:
puts saves variable that is inside getsaves
As you’ve written it, not only is the
savesvariable inaccessible from outside the class scope, it goes out of scope at the end of thegetsavesmethod.You should do something like this instead:
Now you can simply use
j.savesto access the@savesvariable.