Lloyd Zusman wrote:
> I'm wondering if something like this is possible:
>
>   module Intercept
>     def intercept_initialize
>       puts "performing Intercept#intercept_initialize"
>       # somehow call containing class's 'initialize' method here
>     end
>   end
>
>   class Foo
>     def initialize
>       puts "performing Foo#initialize"
>     end
>   end
>
>   class Bar
>     include Intercept
>     def initialize
>       puts "performing Bar#initialize"
>     end
>   end
>
>   puts "instatiating Foo"
>   Foo.new
>   puts "instatiating Bar"
>   Bar.new
>   puts "done"
>
> Desired results:
>
>   instantiating Foo
>   performing Foo#initialize
>   instantiating Bar
>   performing Intercept#intercept_initialize
>   performing Bar#initialize
>   done
>
> In other words, simply by including the "Intercept" module in a
class,
> I'd like to intercept that class's call to "initialize" and have the
> Intercept module's code get invoked first, and _then_ the containing
> class's "initialize" method should be called as it normally would.

Simply redefine IncludingClass::new like this:

.. module Intercept
..   def self.included(mod)
..     super(mod)
..     def mod.intercept_initialize
..       puts "performing Intercept#intercept_initialize"
..       # somehow call containing class's 'initialize' method here
..     end
..     def mod.new *args
..       inst = super *args
..       intercept_initialize
..       inst
..     end
..   end
.. end
..
.. class Foo
..   def initialize
..     puts "performing Foo#initialize"
..   end
.. end
..
.. class Bar
..   include Intercept
..   def initialize
..     puts "performing Bar#initialize"
..   end
.. end
..
.. puts "instatiating Foo"
.. Foo.new
.. puts "instatiating Bar"
.. Bar.new
.. puts "done"