From: Robert Dober Date: 2008-06-28T01:59:43+09:00 Subject: Re: Is it ellegant to use a global variable to store a Logger object? On Fri, Jun 27, 2008 at 11:40 AM, Robert Dober wrote: > On Fri, Jun 27, 2008 at 10:17 AM, Shot (Piotr Szotkowski) wrote: >> Iñaki Baz Castillo: >> >>> El Jueves, 26 de Junio de 2008, Shot (Piotr Szotkowski) escribió: >> >>>> module MyProgram >>>> class Logger >>>> class << self >>>> def debug … >>>> … >>>> end >>>> end >>>> end >>>> end >> >>> Ok, this seems really ellegant and I've tested that I can call >>> "Logger.debug..." in any submodule/subclass into the program :) >> >> :) >> >>> Just a question: Logger must use an instance of a class (real >>> class Logger), so to store it I think the best option is using >>> a @@class_variable into Logger module >> >> No – by using class << self, you're operating on the Logger object >> (an instance of the class Class), and you can access its *instance* >> variables. > > Amen > > grep --recursive '@@' lib && echo "Classvariables are the root of all evil ;)" > > Cheers > Robert > They are shared by all subclasses, look at this irb(main):006:0> class A irb(main):007:1> @@a = self irb(main):008:1> end => A irb(main):009:0> class B < A irb(main):010:1> @@a = self irb(main):011:1> end => B irb(main):013:0> A.send :class_variable_get, :@@a => B this was bad enough for me never wanting have to do with them again, now look at this irb(main):014:0> class A irb(main):015:1> @a = self irb(main):016:1> end => A irb(main):017:0> class B < A irb(main):018:1> @a = self irb(main):019:1> end => B irb(main):020:0> A.instance_variable_get :@a => A irb(main):021:0> B.instance_variable_get :@a => B HTH Robert -- http://ruby-smalltalk.blogspot.com/ --- AALST (n.) One who changes his name to be further to the front D.Adams; The Meaning of LIFF