Phrogz
8/16/2007 6:56:00 PM
On Aug 16, 10:25 am, Alex Young <a...@blackkettle.org> wrote:
> Arno J. wrote:
> > class A
> > @class_instance_variable
> > class << self #or class << A
> > @singleton_variable # <- Can this be used/called elsewhere ?
> > def some_method
> > @class_instance_variable
> > end
> > end
> > end
> class A
> class << self
> @singleton_variable
> attr_accessor :singleton_variable # <----- N.B.
> end
> end
> irb(main):035:0> A.singleton_variable = :foo
> => :foo
> irb(main):037:0> A.singleton_variable
> => :foo
Your accessor method there is referencing a new class-level instance
variable, that is in a different scope than your (no-op)
@singleton_variable line there.
irb(main):001:0> class A
irb(main):002:1> @foo = 1
irb(main):003:1> class << self
irb(main):004:2> @foo = 2
irb(main):005:2> attr_accessor :foo
irb(main):006:2> end
irb(main):007:1> end
=> nil
irb(main):008:0> A.foo
=> 1