对不起新手问题,但我如何传递/访问 attar_accessor 到多个类?在下面的示例中, Foo 类永远无法从 Level 类中看到更新的值。
class Level
attr_accessor :level, :speed
end
class Foo
attr_reader :a, :b
def initialize
@x = Level.new()
@a = @x.level
@b = @x.speed
end
def reload
@a = @x.level
@b = @x.speed
end
end
@testa = Foo.new()
@testb = Level.new()
@testb.level = 5
@testb.speed = 55
puts "Value for attr_accessor is:"
puts "#{@testb.level}"
puts "#{@testb.speed}"
puts "#{@testa.a}"
puts "#{@testa.b}"
@testa.reload
puts "#{@testa.a}"
puts "#{@testa.b}"