From: Francois Goret Date: 2002-12-06T22:34:02+09:00 Subject: Re: Class-instance variables access Hi Greg, Thanks for your help. In fact the question was a little bit different: you can define IN A CLASS what's Guy called "class instance variables". I was stunned by this a few days ago, as I was assuming too that there are only "class variables" and "instance variables", the ones you described. Class instance variables seems to be a third kind of animal: variables stored in the class (not in it's instances), and who does support the following construct: class B def B.test @x end end class A < B @x = 12 # not @@x = 12 end A.test => 12 If you do the same with @@x, it doesn't work as the class method B.test will try to access the non-existant @@x IN B, not in A. So I'm really puzzled by these class instance variables... and I didn't find a way to access their values from an instance. Thanks again for your help, Francois On Friday 06 December 2002 20:08, Greg Millam wrote: > > I try to understand the difference between 'class variables' and 'class > > instance variables', following a post by Guy Decoux a few days ago. > > If you've ever worked with C or Java, a class variable is like a variable > declared 'static' > > class A > def intialize(y) > @x = y > @@x = y > end > def test > puts "@x: #{@x}" > puts "@@x: #{@@x}" > end > end > > foo = A.new("foo") > bar = A.new("bar") > > foo.test > bar.test > > - You get: > @x: foo > @@x: bar > @x: bar > @@x: bar > > @x is an instance variable that each instance stores it's own version of > @@x is a class variable that is stored in the class definition > > @x can be different from instance to instance, @@x is the same for all > instances.