Re: Overriding new?
- From: Chuck Remes <cremes.devlist@xxxxxxx>
- Date: Mon, 15 Mar 2010 12:24:15 -0500
On Mar 15, 2010, at 12:07 PM, Tony Arcieri wrote:
On Mon, Mar 15, 2010 at 7:41 AM, Andrea Dallera <andrea@xxxxxxxxxxxxxxxxx>wrote:
Hei Chuck,
that's exactly what i was trying to avoid.
One way to avoid it is to override new. You can use the "allocate" method
to create a new object instance without invoking its constructor. Name the
superclass's "initialize" method something different, like preinitialize,
and invoke that yourself. Be sure to invoke initialize too, if it's
defined.
I've used this approach in frameworks where I intend for users to subclass,
but my general impression nowadays is I should use a lot cleaner base class
that doesn't need these sorts of hax, or not expect users to ever define
their own constructor.
I like Tony's final suggestion. In your base class initialize method, have it make a call to post_initialize. All subclasses should then create a post_initialize method if they need to do any construction (or leave it empty).
This technique is used in Eventmachine since its internals like to control when and how classes are instantiated.
cr
.
- Follow-Ups:
- Re: Overriding new?
- From: Andrea Dallera
- Re: Overriding new?
- References:
- Overriding new?
- From: Andrea Dallera
- Re: Overriding new?
- From: Chuck Remes
- Re: Overriding new?
- From: Andrea Dallera
- Re: Overriding new?
- From: Tony Arcieri
- Overriding new?
- Prev by Date: Re: Overriding new?
- Next by Date: Re: Where is Ruby 1.9 'TK' library?
- Previous by thread: Re: Overriding new?
- Next by thread: Re: Overriding new?
- Index(es):
Relevant Pages
|