Published 2012-05-07 @ 17:00
Found via a rails SNAFU:
# in rails spread across many modules and classes :( class A def self.inherited(x) p x.instance # create a singleton upon inheritance end def self.instance @instance ||= new end def initialize p :A_initialize end end # in your application.rb class B < A def initialize p :B_initialize end end
This prints out
:A_initialize only.. and is terribly confusing
because it is about 8 layers of indirection deep. tenderlove and I
poked at this for about 30 minutes trying to untangle why his simple
initialize method wouldn’t get called.
Turns out it is that crafty instantiation inside of the
inherited triggers on the class opening… when
hasn’t been defined yet.
This sucks big time and is a perfect illustration of why I think ruby needs a hook for the close of a inherited class, not just the opening. So many powerful tools could be written with such a simple hook. I have yet to see inherited be used for much more than the horrible cargo-culted ClassMethods+InstanceMethods pattern.
I found a workaround… It sucks, but it does ensure that your
initialize gets called: