From: Paul Date: 2006-08-21T06:25:08+09:00 Subject: Re: Class variables in Ruby Jan and David: Yes, this makes perfect sense now that you mention it. I had not seen anything written up on it yet. Thanks for the examples. Paul Jan Svitok wrote: > On 8/19/06, Paul wrote: > > Hi Gary, > > > > I'm not quite sure what you mean by using "an instance variable of the > > class object". I am familiar with class variables and instance > > variables. In my case I wanted to cache the data in a class variable > > because *any* instance could reference the single class variable. Is > > there yet another type of variable (other than temporary method scope > > variables) that one could define? > > > > Thanks, > > Paul > > This comes from Ruby's object model (and this may sound familiar to > you as a smalltalker): in Ruby, everything is an object, so any class > is an object as well. A class is an instance of class Class. Therefore > any class can have its instance variables (because it is an object) in > addition to class variables (class' special behaviour). > > Class variables (@@variable) are shared among all sublasses of the class. > > Class instance variables are those, that are defined in class methods i.e. > > class A > def self.m() > @a = 1 # <- HERE! > end > end > > or: > > class A > class << self > def m() > @a = 1 # <- HERE! > end > end > end > > These are not shared among subclasses. > > HTH, > J.