From: Ralph Shnelvar Date: 2009-11-28T08:00:32+09:00 Subject: Re: Class variables, instance variables, singleton; Ruby v. C++ DM> On Friday 27 November 2009 01:07:45 pm Ralph Shnelvar wrote: >> Newb here coming from C++ >> >> Ok, I _think_ I know what a class variable is. It is similar to a >> static variable in a class in C++. Rght? DM> Yes. DM> A quick warning, though: Class variables behave weirdly with inheritance. DM> Avoid them. Since all Ruby classes are themselves objects, it makes more sense DM> to define an instance variable of the class. DM> That is, instead of doing this: DM> class Foo DM> def bar DM> @@some_count += 1 DM> end DM> end DM> Do this: DM> class Foo DM> def self.increment_count DM> @some_count += 1 DM> end DM> def bar DM> self.class.increment_count DM> end DM> end DM> If you understand how that works, you should be able to understand this DM> example, which is how I'd actually do it: I am clueless what all of the above does. It is WAY over my head. I have read that section on how self is set in the Pickaxe book a dozen times ... and I'm still lost. How would I go about deciphering the meaning of what you wrote, above? DM> class Foo DM> class << self DM> attr_accessor :some_count DM> end DM> def bar DM> self.class.some_count += 1 DM> end DM> end And this is even more obscure to me.