From: Ruby Admirer Date: 2007-02-09T19:48:37+09:00 Subject: Re: What is really a class instance variable compare to a class variable ? --0-1289419354-1171016640=:43351 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit David, Thanks a lot for your reply. But this raises another understanding problem for me. It seems that regarding inheritance there are different behaviours for : - an object instance variable - a class variable - a class instance variable For the following code (I'm running Ruby 1.8.5p12) : class TestObject def initialize @var = "an object instance variable" end def print puts @var end end class DerivedTestObject < TestObject def set_var @var = "an object instance variable : changed" end end o = DerivedTestObject.new o.print o.set_var o.print The output is : an object instance variable an object instance variable : changed => So, an object instance variable is shared/seen by the derived object instances. For the following code : class TestClass @@var = "a class variable" @var = "a class instance variable" def self.print puts @@var puts @var end end class DerivedTestClass < TestClass def self.set_var @@var = "a class variable : changed" @var = "a class instance variable : changed" end end TestClass.print puts "-------------" DerivedTestClass.print puts "-------------" DerivedTestClass.set_var DerivedTestClass.print puts "-------------" TestClass.print The output is : a class variable a class instance variable ------------- a class variable nil ------------- a class variable : changed a class instance variable : changed ------------- a class variable : changed a class instance variable => So, a class variable is shared/seen by derived classes (similar behaviour as an instance variable). Conversely, a class instance variable is not shared/seen by derived classes. Am I missing something ? Another question : The inheritance of a class variable and a class method is really great :-). Is it going to be removed in Ruby 2.0 ? I heard something about that but I hope not. Thanks for any explanation. CM. dblack@wobblini.net wrote: HI -- On Fri, 9 Feb 2007, Ruby Admirer wrote: > Hi all, > > Given the following definition : > > class A > @@class_var = "a class variable" > @class_instance_var = "a class instance variable" > > def a_method > @instance_var = "an instance variable" > end > end > > I understand what is a class variable and what is an instance > variable. The problem I have is what is a class instance variable > like @class_instance_var ? Where does this variable live ? > > A class variable is shared by all instances of the class. But what > about the class instance variable ? Every instance variable you ever see belongs to whatever object is, at the moment you see the instance variable, playing the role of 'self'. Inside a class definition block, 'self' is the class object itself: class A p self # A end So if you have an instance variable at that point in execution: class A @x = 1 end it belongs to the object A, which is a class object. Similarly, that variable @x will only be visible when self is A. When self is not A, @x will not be that same instance variable. You can always establish ownership of instance variables by calculating what self is at the point that the instance variable appears. Keep in mind, too, that the phrase "class instance variable" is just a way of clarifying that you don't mean "instance variable defined in this class's instance methods". There's no separate "class instance variable" construct at the language level; there are just objects, some of which are class objects and all of which can be self and therefore have instance variables. David -- Q. What is THE Ruby book for Rails developers? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf) Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.com) --------------------------------- Check out the all-new Yahoo! Mail beta - Fire up a more powerful email and get things done faster. --0-1289419354-1171016640=:43351--