I have an initialize method I want to run at the end of any daughter or
granddaughter ‘initialize’ to make sure the state has been created
properly, and
I would rather specify the execution from the base class itself than
count on
those descendents to do it.

I have an initialize method I want to run at the end of any daughter or granddaughter ‘initialize’ to make sure the state has been created properly, and I would rather specify the execution from the base class itself than count on those descendents to do it.

The most correct way to do it is to have all subclasses properly use
super at the end of their initialize bodies:

class Subclass < Superclass
def initialize
# … stuff
super
end
end

There are “fancier” ways (read: overly clever), but doing it this way is
cleaner, faster, and easier to debug / maintain.

There are “fancier” ways (read: overly clever), but doing it this way is cleaner, faster, and easier to debug / maintain.

The problem with that is you may want some things from super available
before
you do other things in initialize. I figured out another way, which is
to
define initialize subroutines as pure virtual in the base class, called
from
initialize, then I put the thing at the end of initialize, and all the
daughters
only modify the called initializers, and all use the base class
initialize
method. There is nothing to force daughters not to make their own
initialize,
but at least this is nice and formal.

It would be interesting to see how much this meta-aspect could be
included into ruby - instead of writing ruby code per se, write hooks
upon hooks that get triggered when this or that changes. Hmm… sounds
like another language though.

I have an initialize method I want to run at the end of any daughter or
granddaughter ‘initialize’ to make sure the state has been created
properly, and
I would rather specify the execution from the base class itself than
count on
those descendents to do it.

This is a designed-in feature of Common Lisp, where you can define
:after methods which do what you describe (there’s also :before and
:around methods too).

I thought I saw Ruby 2.0 prototype code for something similar (:pre and
:post methods?), but I can’t seem find the reference now.

A possible long-term solution (which begins as an experiment) is to go
the whole nine yards: design the spec for :pre and :post methods (maybe
:around too), implement it, and publish the gem. Refine until it starts
to crystallize. Then use the gem.

(It’s not overly clever if there’s a clean API together with a boatload
of tests, in my opinion.)

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

end
initialize, then I put the thing at the end of initialize, and all the daughters
only modify the called initializers, and all use the base class initialize
method. There is nothing to force daughters not to make their own initialize,
but at least this is nice and formal.

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.

I have an initialize method I want to run at the end of any daughter
or granddaughter ‘initialize’ to make sure the state has been created
properly, and I would rather specify the execution from the base class
itself than count on those descendents to do it.

Other than what others already said, you could try either an AOP
solution like Aquarium or Gazer (both seem unmaintained) or RCapture
(my personal favourite), which seems to be alive.

(Note this is off the top of my head so it may need tweaking to
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.

I was wondering where my concrete dildo went. Please clean the Fruit
Loops off and return ASAP. My lawn ornaments just aren’t the same
without it.