From: Christoph Rippel Date: 2001-03-14T02:40:02+09:00 Subject: [ruby-talk:12575] Re: class variables "Dave Thomas" wrote in message news:m2elw2xmbw.fsf@zip.local.thomases.com... [...] > I assume you mean instance variables within the class yes > > -- variable local to the Meta Class > > I have no idea what this is :) Instance variable within in the meta class - i.e. the singleton class of the class object. > > > From your examples, I'm not sure I see where the confusion is. The Yes confusing was not the right word - it is very consistent but it takes time to get used to variables being created on a need base (this was very confusing to me in the beginning - you might want to throw in an extra page of examples to illustrate this behavior if you every make a major rewrite of your overall great book - but my problems with this might have been singular ... ) > only possible strangeness appears to be the expectation that > > class Fred > @var = 123 > end > > sets an instance variable for objects of class Fred, rather than for > Fred itself. I guess this was my main point of the post - I find this quite usefull. Instance variables of meta classes are created the same way (I don't think you will find much use for this - I was simply wandering were @var ``lives'' ) class << Fred @var = 234 # creates instance variable of meta-class end The question is how get a handle on the meta class this is what MetaFred = ObjectSpace._id2ref ( class << Fred id end ) does. Now I can define MetaFred method which has access to this variable def MetaFred.var @var end that is p MetaFred.var # => 234 Note that evenso MetaFred.var # => Class you still have p MetaFred == Class # => false Class variables on the `'singleton - level'' obviously don't exist (but I checked to make sure anyway.) > When I think about classes in Ruby, I have a very simple model. There > really is only one basic structure: an instance with some state, and > chained off it a list of classes,each of which contains a list of > methods. Then there's the twist: classes are object instances too, and > so have their own state and list of classes, and so on. Classes that > Ruby generates internallyare called singletons, and are mostly the > same as any other class, except they're invisible. > > Then there's just one more thing: when defining a class, you're > executing code in the context of that class object: > > class Dave > p self #=> Dave > end > > Instance variables are always defined in terms of the current value of > self. What really cleared a lot things up for me was the realization that you can only trust the id of an object - that is you have to look at class Christoph p id # => ..... end > Once this picture was clear, I found that I can understand pretty > intuitively what the scope of various constructs should be. I tried > to set all this down in chapter 19. However, this just represents my > own warped way of thinking: I'm sure others will have different ways > of visualizing the hypercube :) Talking about hypercubes - it is actually possible to create Meta Freds of any order - One simply has to put a handle on the previous level - it is really amazing that everything works out perfectly - really neat. I don't think it possible to get a handle on the proxy class of a module include put who knows. [...] Christoph