From: Christoph Rippel Date: 2001-03-14T03:32:45+09:00 Subject: [ruby-talk:12578] Re: class variables > From: ts [mailto:decoux@moulon.inra.fr] [...] > C> The question is how get a handle on the meta class this is what > C> MetaFred = ObjectSpace._id2ref ( > C> class << Fred > C> id > C> end ) > C> does. Now I can define MetaFred method which has access to this variable > > C> def MetaFred.var > C> @var > C> end > > C> that is > > C> p MetaFred.var # => 234 > > I'm probably stupid but why you don't write ? > > class Fred > @var = 234 > end > > def Fred.var > @var > end > > p Fred.var Because they are NOT the same - if you accidentally write class << Fred @will_vanish = "bye" end were to think (you know this of course) the variables lives - it isn't an instance variable of an object of type Fred, nor is it an instance variable of the class object Fred - it must live somewhere else - this is the metaclass (manipulating things on the first meta-level might actually be useful beyond this it is obviously nonsense;-) > > variable with the prefix '@@' was introduced because it's was not possible > to access a class variable in a method, i.e. > > class Fred > def toto > @var # this is always an instance variable > end > end Funny, I just used exactly this behavior of class variables in my interlude post ... [...] Christoph