Ruby 类访问器返回nil class-Foo 类Sergio,也许值得注意的是,@var=“bar”也可以使用。@CarySwoveland它值得注意。感谢您的关注:) class Foo class << self attr_acces
类访问器返回nilRuby 类访问器返回nil class-Foo 类Sergio,也许值得注意的是,@var=“bar”也可以使用。@CarySwoveland它值得注意。感谢您的关注:) class Foo class << self attr_acces,ruby,Ruby,类访问器返回nil class-Foo 类Sergio,也许值得注意的是,@var=“bar”也可以使用。@CarySwoveland它值得注意。感谢您的关注:) class Foo class << self attr_accessor :var end end class Bar < Foo var = "bar" p var # print "bar" end p Bar.var # print nil class Bar < Fo
class-Foo
类Sergio,也许值得注意的是,@var=“bar”
也可以使用。@CarySwoveland它值得注意。感谢您的关注:)
class Foo
class << self
attr_accessor :var
end
end
class Bar < Foo
var = "bar"
p var # print "bar"
end
p Bar.var # print nil
class Bar < Foo
var = "bar" # this is assignment to local variable, not the accessor
end
class Foo
class << self
attr_accessor :var
end
end
class Bar < Foo
self.var = "bar"
var # => "bar"
end
Bar.var # => "bar"