Thomas said:
Or something like:
require 'facets/kernel/as'
class MyClass
class << self
alias _new new
def new
instance = _new
ancestors.reverse_each do |anc|
if anc.instance_methods(false).include?
preinitialize)
instance.as(anc).preinitialize
end
end
end
end
end
Then define #preinitialize at any level you wish.
(Note this is off the top of my head so it may need tweaking to
run).
Yeah, that was Ryan's point about overly clever solutions. That may
look good now, but someone who is debugging some code which depends on
it will hate you. And when you are debugging six months from now,
you'll hate yourself.
If you really want to do this, put some real effort into designing a
clean API for it. Put it in a gem. Make it real, real obvious that
you're putting a twist on the conventional method calling
chain. Document the API so it's obvious in a coherent, readily
understandable way.
Don't slip a concrete dildo into someone's box of Fruit Loops. They
won't be happy with your Morning Breakfast Surprise. Put the concrete
dildo in a clearly labeled box, with instructions. Then when someone
encounters a problem, "Hey, something is screwing me here. Maybe it's
the concrete dildo?" at least they know to ask.