From: "Iñaki Baz Castillo" Date: 2008-06-27T23:49:48+09:00 Subject: Re: Is it ellegant to use a global variable to store a Logger object? El Viernes, 27 de Junio de 2008, Shot (Piotr Szotkowski) escribió: > 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. Thanks, in fact I didn't understand the meaning of "class << self" until now. > That’s the other elegant part about this solution – you can > use anything you’re used to, including, for example, attr_accessors. > > I use this approach like in the below code; I can use stuff like > > $stderr.puts 'some debug line' if Config.debug > > 1.upto Config.max_pins do |pin| > … > end > > or decide whether to use Enumerable#map or the forkoff > gem based on whether Config.processes is one or more. :) This seems really cool. I'll try to use it. Thanks a lot. -- Iñaki Baz Castillo