From: ahoward Date: 2003-01-23T00:20:45+09:00 Subject: Re: accessors for module instance vars On Wed, 22 Jan 2003, Mauricio Fern�ndez wrote: > > module M > > attr_accessor :x > > @x = 42 > Doesn't refer to the same thing! > The former will be an iv. of objects extended with M _or_ > instanciated from classes that include M. > The later is a module instance variable, ie, an instance variable > associated to the 'M' object (that of class Module). > > end > > > > class Foo; include M; end > > > > p Foo.new.x # >> nil > > Because it's not the same @x! that was my point. > irb(main):001:0> module M > irb(main):002:1> class << self; attr_accessor :x; end > irb(main):003:1> end > nil > irb(main):004:0> M.x = 1 > 1 > irb(main):005:0> M.x > 1 didn't know you could do that... > The following won't work: > > irb(main):013:0> class Foo; include M; end > Foo > irb(main):014:0> Foo.x > NameError: undefined method `x' for Foo:Class > from (irb):14 > irb(main):015:0> Foo.new.x > NameError: undefined method `x' for # > from (irb):15 yes. all the more reason this idea : > > And if I leave it as a module it could be mixed into a class later (though > > that's not how I'm using it now) and all of the 'module instance > > variables' would also end up in the class it was mixed into.... will not work. > The only ways to access module instance variables are through the > accessors and instance_eval, AFAIK. AFAIK -> ??? -a -- ==================================== | Ara Howard | NOAA Forecast Systems Laboratory | Information and Technology Services | Data Systems Group | R/FST 325 Broadway | Boulder, CO 80305-3328 | Email: ahoward@fsl.noaa.gov | Phone: 303-497-7238 | Fax: 303-497-7259 ====================================