I have a couple of questions... > -----Original Message----- > From: ara.t.howard / noaa.gov [mailto:ara.t.howard / noaa.gov] > Sent: Sunday, July 23, 2006 9:34 PM > To: ruby-talk ML > Subject: Re: Set an instance variable before and after initialize > > On Sun, 23 Jul 2006, Martin Jansson wrote: > > > If possible, I would like to set a instance variable in an > object before and > > after its initialize method is executed. > > let initialize help you do this then: > > > harp:~ > cat a.rb > module BeforeAfterInit > module InstanceMethods > def before_initialize *a, &b > end > def after_initialize *a, &b > end > def initialize *a, &b > before_initialize *a, &b > r = super What does this construction (r=super) do? I tried removing both references to r and replacing it with a single 'super' and it seemed to behave identically. > after_initialize *a, &b > r > end > end > module ClassMethods > def before &b > define_method 'before_initialize', &b > end > def after &b > define_method 'after_initialize', &b > end > end > def self.included other > other.module_eval{ include InstanceMethods } > other.extend ClassMethods > super > end > end > > class C > include BeforeAfterInit > > before{ @a = 40 } > after{ @b = 2 } When I define an initialize method here without using super, it breaks (obvious, I guess), When I use super the before and after code gets run at the same time - in other words: def initialize p @a #should be set, but => nil super end So to my naive understanding this doesn't acheive the goal. What am I missing? > def m() @a + @b end > end > > p C.new.m > > > > harp:~ > ruby a.rb > 42 Cheers, ben