From: ara.t.howard@... Date: 2006-07-27T00:51:34+09:00 Subject: Re: Set an instance variable before and after initialize On Wed, 26 Jul 2006, Ben Nagy wrote: > Oh, I did that too. My point is that there is no 'before' and 'after' the > code gets run at the same time and it's not possible to insert other code in > between them, which was presumably the point? hmmm. you can actually do it with my first impl by including the module __after__ defining initialize. but this makes it easier: harp:~ > cat a.rb module BeforeAfterInit module InstanceMethods def before_initialize(*a, &b) end def after_initialize(*a, &b) end end module ClassMethods def before &b define_method 'before_initialize', &b private 'before_initialize' end def after &b define_method 'after_initialize', &b private 'after_initialize' end def new(*a, &b) (obj = allocate).instance_eval{ before_initialize initialize *a, &b after_initialize self } end end def self.included other other.module_eval{ include InstanceMethods } other.extend ClassMethods super end end class C < ::Array include BeforeAfterInit before{ self << :before } after{ self << :after } def initialize self << :initialize end end p C.new harp:~ > ruby a.rb [:before, :initialize, :after] regards. -a -- suffering increases your inner strength. also, the wishing for suffering makes the suffering disappear. - h.h. the 14th dali lama