From: ts Date: 2002-12-06T22:49:33+09:00 Subject: Re: Class-instance variables access >>>>> "F" == Francois GORET writes: F> I understand that @@x refers only to a (non existant) class variable of A. I F> don't catch why there is a distinction between a class instance variable and F> a class variable: the two are "stored" in the unique object representing the F> class A, no ? Well, what you must understand is just this pigeon% ruby -e 'p Array.ancestors' [Array, Enumerable, Object, Kernel] pigeon% See Object in the inheritence. in ruby a class is an object like any other object. Now when you write this class A def initialize @a = 12 end end a = A.new You have created an object of class A, and you have defined an instance variable for this object. Only this object can access its instance variable and if you want to make it "public" you define an accessor like this class A attr_accessor :a def initialize @a = 12 end end a = A.new a.a = 24 This work for *all* objects, and because it work for all objects it work also for a class (a class is an object). this mean that you can write class A @b = 12 end and you have defined an instance variable for the object A, and only A can access it. Like previously, if you want to make it "public" you must create an accessor, something like this class A class << self attr_accessor :b # attr_accessor is for the class, not the # instance, this is why I've used class << self end @b = 12 end A.b = 24 and now you can access the instance variable of A and you can write class A class << self attr_accessor :b end @b = 24 attr_accessor :a def initialize @a = 12 end def show puts "class instance variable #{self.class.b}" puts "instance variable #{self.a}" end end a = A.new a.show Guy Decoux