From B
Execpt now you’ve just changed A which could break anything else using
it.
Actually I think J’s use of initialize, rather then some other method
was in a err. Yes? You can skip over ancestors using something like
facet/kernel/as but initialize is a touchy matter --it’s not something
one generally defines in a module in the first place.
Even so, if that’s really wha is wanted…
require ‘facet/kernel/as’
class C
include A
def initialize
as(B).initialize
end
end
What does #as do? It get the UnboundMethod in B and binds it to C, and
offers it up in a nice little Functor. You can bypass the Functor if
you want with #send_as(B,:initialize).
You can skip over ancestors using something like
facet/kernel/as but initialize is a touchy matter --it’s not something
one generally defines in a module in the first place.
I see this idea mentioned often (avoid initialize in modules). Some
modules
need initialization, some don’t. I don’t think it is helpful
to generalize beyond that. I often see:
class B
def initialize
super
# init for B here
end
end
You can use Module#included to handle class/module level
initialization but
I sometimes wish there was a well-defined instance initialization
hook for
included modules instead of relying on the appropriate use of super()
within
initialize(). I’m not sure what it would look like though, maybe:
module M
def self.instantiated(x)
x.initialize_M
end
def initialize_M #initialization for M goes here
end
end
Presumably Class#new would trigger Module.instantiated for all
included modules
in some well defined order (appearence in ancestors list?).
These seems kind of inelegant though. There must be a better
solution. Or maybe
it is a solution looking for a problem.
def nesting
else
def initialize; puts “From A”; end
end
C.new
#=> From B
#=> From A
This code essentially (or at least partially succeeds at) reversing the
inclusion order of two modules. Ie.
module B; end
module A; include B; end
becomes
module B; include A; end
Are you sure you want to do that? That can have crazy effects! In fact
it’s a very bad idea unless you have full control coding over these
modules, and if that’s the case you wouldn’t need to do it anyway. So
as I say, I’m highly suspect. Looking at exacly what this does it
appears that: