From: Michael Schuerig Date: 2007-12-17T05:14:06+09:00 Subject: Re: Mixins and class variables On Sunday 16 December 2007, Brubix wrote: > I can't figure out how to set class variables from class methods > inherited from a module ? Consider if class variables are really what you want. $ irb >> module M >> def set(x) >> @x = x >> end >> def get >> @x >> end >> end => nil >> class C >> extend M >> end => C >> C.get => nil >> C.set('bla') => "bla" >> C.get => "bla" >> class D < C; end => nil >> D.get => nil @x in this case is not a class variable, it is an instance variable of the singleton class of class C. You'll find some information on that in the Pickaxe. The notable difference compared to class variables proper is that class variables are shared in the inheritance hierarchy, whereas singleton class instance variables are not. $ irb >> class C >> def self.set(x) >> @@x = x >> end >> def self.get >> @@x >> end >> end => nil >> C.get NameError: uninitialized class variable @@x in C from (irb):6:in `get' from (irb):9 >> C.set('bla') => "bla" >> C.get => "bla" >> class D < C; end => nil >> C.get => "bla" >> D.set('foo') => "foo" >> C.get => "foo" Michael -- Michael Schuerig mailto:michael@schuerig.de http://www.schuerig.de/michael/