T
trans. (T. Onoma)
It is rather a common occurrence that I find myself creating a mixin module
that has instance vars in it, for example:
module Example
def add( ex )
@examples << ex
end
end
class X
include Examples
end
Now the class that includes this module requires @examples to be predefined.
But short of adding @examples = [] to the #initialize method of the class
(which would ruin the goal of using the module to begin with) one ends up
doing something like:
module Example
def add( ex )
( @examples || [] ) << ex
end
end
But this seems quite computationally wasteful to me. Another alternative is to
add an intialize to the module and a super call to the class:
module Example
def initialize
@examples = []
end
def add( ex )
@examples << ex
end
end
class X
include Examples
def initialize
super()
end
end
But again, by having to modify the class we're defeating the one of the basic
purposes of module "componentization": SOC.
So I was thinking/wondering if it were possible to have an a variant
initializer that _always_ calls super? it would make sense to have one for
both begin and end positions. So the above example becomes:
module Example
def initialize_pre
@examples = []
end
def add( ex )
@examples << ex
end
end
class X
include Examples
end
When X.new is called initialize_pre automatically proceeds the #initialize
method. #initialize_post would do the same for when the #initialize method
finishes. Of course these would automatically work their way up the entire
class/module chain.
BTW this is not necessarily the same as method wraps (consider the subtle
differences) --rather these are strictly callbacks of initialization.
Thoughts?
T.
that has instance vars in it, for example:
module Example
def add( ex )
@examples << ex
end
end
class X
include Examples
end
Now the class that includes this module requires @examples to be predefined.
But short of adding @examples = [] to the #initialize method of the class
(which would ruin the goal of using the module to begin with) one ends up
doing something like:
module Example
def add( ex )
( @examples || [] ) << ex
end
end
But this seems quite computationally wasteful to me. Another alternative is to
add an intialize to the module and a super call to the class:
module Example
def initialize
@examples = []
end
def add( ex )
@examples << ex
end
end
class X
include Examples
def initialize
super()
end
end
But again, by having to modify the class we're defeating the one of the basic
purposes of module "componentization": SOC.
So I was thinking/wondering if it were possible to have an a variant
initializer that _always_ calls super? it would make sense to have one for
both begin and end positions. So the above example becomes:
module Example
def initialize_pre
@examples = []
end
def add( ex )
@examples << ex
end
end
class X
include Examples
end
When X.new is called initialize_pre automatically proceeds the #initialize
method. #initialize_post would do the same for when the #initialize method
finishes. Of course these would automatically work their way up the entire
class/module chain.
BTW this is not necessarily the same as method wraps (consider the subtle
differences) --rather these are strictly callbacks of initialization.
Thoughts?
T.