From: Devin Mullins Date: 2005-07-03T05:05:37+09:00 Subject: Re: class variable leading a double life James Edward Gray II wrote: > If you want it to be the same variable in both places, you need a > class variable: > > class Test > @@var = 0 > > ... > > James Edward Gray II OK, now I'm confused. I had thought that class variables were just instance variables of the Test object, but it seems that the case is much weirder than that. (Why?) class Test @fun = "hello" @@fun = 5 def initialize p @fun p @@fun @fun = "blah" @@fun = "bloo" end def fun p @fun p @@fun end end def Test.fun p @fun p @@fun @fun = "bing" @@fun = "bong" end irb(main):021:0> Test.fun "hello" NameError: uninitialized class variable @@fun in Object from (irb):17:in `fun' from (irb):21 irb(main):022:0> t = Test.new nil 5 => # irb(main):023:0> Test.fun "hello" NameError: uninitialized class variable @@fun in Object from (irb):17:in `fun' from (irb):23 irb(main):024:0> t.fun "blah" "bloo" => nil 1. Class variables are these odd things that are shared among every instance of Test. 2. Class variables of Test are not available to class methods of Test itself. 3. The singleton class for Test can have instance variables, but not class variables.* 4. Why didn't the second Test.fun call print "bing"? 5. Is there some way to share data between between a class's instance methods and its class methods? I'm going to go study the PickAxe2 now, but I figured maybe I could get a quicker answer from some of you. :) Devin *I'm referring to the anonymous subclass of Class that is the class of the Test constant itself. She sells sea shells by the sea shore.