Trans
11/27/2007 2:24:00 PM
On Nov 27, 5:15 am, Chris Richards <evilgeen...@gmail.com> wrote:
> Wow, thanks for all your replies.
>
> > BTW: To ensure a routine always runs on initialization despite
> > #initialize, one way:
>
> > class X
> > class << self
> > def new(*a, &b)
> > o = allocate
> > o.do_your_thing
> > o.initialize(*a,&b)
> > o
> > end
> > end
> > end
>
> > The other is AOP which is a much larger can of worms.
>
> so whats the simplest way to ensure this method was always run ? :
>
> def initialize(x=0, y=0)
> self.x_position=x
> self.y_position=y
> end
Why do you want to thwart super here? You should have very clear
reason for doing so, otherwise leave it.
But to answer your question directly:
class X
class << self
def new(*a, &b)
o = allocate
o.pre_initialize(*a, &b)
o.initialize(*a,&b)
o
end
end
def pre_initialize(x=0, y=0)
self.x_position=x
self.y_position=y
end
end
T.