On Feb 2, 2007, at 12:21 AM, Daniel DeLorme wrote:
> It would be nice if there was a kind of initialize method that
> would be called at every level of the object without a need for
> 'super'

I don't claim that this particular pattern makes sense but you
can override Class#new to do what you want.  You could also
do this on a per/class basis.

class Class
   def new(*a, &b)
     o = allocate
     ancestors.each { |x|
       m = x.instance_method(:once) rescue nil
       m and m.bind(o).call(*a, &b)
     }
     o.send(:initialize, *a, &b)
     o
   end
end

I'm not sure what order it would make sense for visiting the
ancestors classes.  I just called the 'once' method in the
ancestor order.  Should this happen before or after initialize?

Question:  Why does instance_method raise an exception for an
unknown method rather than simply returning nil?


Gary Wright