From: "Gennady F. Bystritsky" Date: 2002-11-15T01:19:28+09:00 Subject: Re: Another Newbie question regarding instance variables? I thought that I knew Ruby pretty well until I came accross this thread about "class instance variables". It always haunted me why it does not work when I do @var = value outside any method definition, meaning initialization of an instance variable. Now I see why. Hence the question: is there any practical use for "class instance variables"? If so, would anybody post an example? Thanks, Gennady Bystritsky. ----- Original Message ----- From: "Joel VanderWerf" To: "ruby-talk ML" Sent: Wednesday, November 13, 2002 11:23 PM Subject: Re: Another Newbie question regarding instance variables? > Aleksei Guzev wrote: > > "montana" wrote in message > > news:200211131633.gADGXeFJ000838@buc99.bsd.st... > > > >>The value of an instance variable is only available to the instance of the > > > > class, whereas the value of the class variable is available to all instances > > of the class? Is this correct? > > > >>If so the power of an instance variable is then to discern between > > > > make-ups of similar instances of the same class. > > > >>Does this sound correct?? > >> > >>Thanks. > >>SA > >> > > > > > > The class itself is an object too. It's an instance of some class. As well > > as other objects it can have its own variables. Those are called "class > > variables" by consideration. But generally they are instance variables too. > > You should take a closer look at the notion of metaclasses. > > Actually, it's clearer to call them "class instance variables". > > class C > @civ = 0 # class instance variable > @@cv = 1 # class variable > def foo > @iv = 2 # instance variable > p @civ > p @@cv > p @iv > end > end > > > c = C.new.foo > # ==> nil because c has no @civ defined > # 1 shared by C and c > # 2 > > > > >