From: luisealvarezb Date: 2009-09-19T11:15:09+09:00 Subject: Re: How to derive from DateTime? On Sep 18, 7:19 pm, Ehsanul Hoque wrote: > > It looks to me that this is a case where you might be better served by using > > composition and delegation rather than inheritance: instead of an Event > > being a specialized DateTime, it would have a DateTime instance as an > > instance variable and delegate certain methods (e.g., year, etc.) to that > > instance variable. > > I agree with Christopher here. What you want is a proxy class. Here's a way you might implement it: > > class Event > >   def self.new( *args ) > >     class << datetime = DateTime.new(*args) > >       def sweet > >         "what a sweet method" > >       end > >     end > >     datetime > >   end > > end > > You could also try this way: > > class Event >     instance_methods.each { |m| undef_method m unless m =~ /(^__|^send$|^object_id$)/ } > > def initialize(*args) > >   @date_time = DateTime.new(*args) > > end > >         def method_missing(name, *args, &block) >           @date_time.send(name, *args, &block) >         end > end > > I'm sure you can figure out what's happening here, and how to merge it with your own code. > > - Ehsan > > _________________________________________________________________ > Bing™  brings you maps, menus, and reviews organized in one place.   Try it now.http://www.bing.com/search?q=restaurants&form=MLOGEN&publ=WLHMTAG&cre... Thank you for that code it looks like a good solution and will use it. However, I am left wondering why this anomaly with the constructors in DateTime, does that not violate OO practices. How can you not be able to inherit from a Class X and redefine it. Thanks a lot...