From: luisealvarezb Date: 2009-09-19T17:25:05+09:00 Subject: Re: How to derive from DateTime? On Sep 19, 12:58 am, Ehsanul Hoque wrote: > > 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... > > Actually, there's no anomaly really, as far as I know. There's some bug in the code. I have no idea where, your code seems fine to me, and it could just be because of the way the DateTime class was implemented. But you can in fact redefined constructors, and it works fine. Try this for example (just an adaptation of your code): > > class Foo >   def initialize(x) >     @x = x >     @message = "message from foo" >   end >   attr_accessor :message, :x > end > > class Bar < Foo >   class << self >     alias :old_new :new >     def new(x) >       obj = old_new >       obj.send :initialize >       obj >     end >   end > >   def initialize >     @message = "message from bar" >   end > >   attr_accessor :message > end > > f = Foo.new(0) > > f.message   #=> "message from foo" > > f.x       #=> 0 > > b = Bar.new(0) > > b.message       #=> "message from bar" > b.x         #=> nil   (I actually don't understand why this is, someone else might be able to explain) > > _________________________________________________________________ > 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... Specializing other classes works fine but DateTime fails...I see, yeah I can't explain it either. Ok, I will just go with what I have. Thanks a lot.