2008/6/24 Iki Baz Castillo <ibc / aliax.net>:
> El Martes, 24 de Junio de 2008, Andrea Fazzi escribi

>> require 'logger'
>>
>> class Container
>>   def logger
>>     @logger ||= Logger.new STDOUT
>>   end
>>   def foo
>>     Foo.new(logger)
>>   end
>> end
>>
>> class Foo
>>   def initialize(logger)
>>     @logger = logger
>>   end
>>   def bar
>>     @logger.info('Foo#bar invoked.')
>>   end
>> end
>>
>> c = Container.new
>> c.foo.bar
>
> That's a cool solution :)

I do not think so.  Reasons: it clutters every instance with a
reference which can have a significant impact on memory if there are a
lot objects.  Then, you have to change a class's #initialize signature
for all classes that do want to do logging and also manually code the
assignment inside the class.  Accessing a logger from a global context
(whichever way you do it) saves memory and is less tedious.

Kind regards

robert



-- 
use.inject do |as, often| as.you_can - without end