From: Chuck Remes Date: 2010-03-16T02:24:15+09:00 Subject: Re: Overriding new? On Mar 15, 2010, at 12:07 PM, Tony Arcieri wrote: > On Mon, Mar 15, 2010 at 7:41 AM, Andrea Dallera 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