From: Daniel Schierbeck Date: 2005-11-24T04:12:25+09:00 Subject: Re: pattern: auto-running module init code Phil Tomson wrote: > In article <43846a75$0$1813$edfadb0f@dread11.news.tele.dk>, > Daniel Schierbeck wrote: > >> Ideally, there would be a method hook that was called when a class >> including the module was instantiated. >> >> module Fooable >> def self.initialized(obj, *args, &block) >> puts "A class including Fooable has been instantiated!" >> obj.do_something(*args) # whatever >> end >> end > > But as Sean pointed out we essentially get the same behavior by calling > 'super' somewhere in the including class's constructor. > > Phil Yeah, but that requires the including class to add code to the `initialize' method, some I don't think should be necessary for a module to function. I think that the behaviour of a module should be kept within that module, unless it needs some specific methods (like Enumerable needs `each'). The only issue I have with my current model (see my other post for an implementation) is that it might be better for the module initializer to be an instance method, instead of a module method. module Fooable def self.initialized(obj, *args, &block); end # versus def initialized(*args, &block); end end And maybe it should be called `instantiated' instead of `initialized', though that doesn't really make much of a difference for me. Cheers, Daniel