From: Morton Goldberg Date: 2007-09-22T09:38:58+09:00 Subject: Re: question re: modules and class variable scope On Sep 21, 2007, at 6:53 PM, Got Ascii wrote: > I spent some time trying to find an answer to this but haven't > found an > explanation I understand. Could someone tell me what is going on > here: > > module Mod > def self.append_features(base) > base.extend ClassMethods > super > end > module ClassMethods > def growl > puts @@donkey > end > end > end > > class Animal > include Mod > @@donkey = "heyhaw" > > def self.working_growl > puts @@donkey > end > end > > # uninitialized class variable @@donkey in Mod::ClassMethods > Animal.growl > > # works fine > Animal.working_growl Class variables are not the same as class instance variables. This is one of those cases where you want a class instance variable. If you change @@donkey to @donkey, everything will work the way you want it to. I wonder why you don't use the much simpler module Mod def growl puts @donkey end end class Animal extend Mod @donkey = "hee-haw" def self.working_growl puts @donkey end end which gives the same result. Maybe your code is a example reduced down from a much more complicated situation? Regards, Morton