# How to derive from DateTime?

**URL:** <https://www.ruby-forum.com/t/how-to-derive-from-datetime/175748>\
**Category:** Ruby\
**Created:** [September 18, 2009, 10:16pm UTC](https://www.ruby-forum.com/t/how-to-derive-from-datetime/175748 "2009-09-18T22:16:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![luisealvarezb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/4bbf92/32.png) [@luisealvarezb](https://www.ruby-forum.com/u/luisealvarezb)\
**Post date:** [September 18, 2009, 10:16pm UTC](https://www.ruby-forum.com/t/how-to-derive-from-datetime/175748/1 "2009-09-18T22:16:01Z")

</div>

Hi, everybody:

I have been tring to derive a class from DateTime and in doing so have  
come across some nasty problems: it turns out overriding the  
constructor is not straight forward at all. I was looking for some  
pointers, anyone???  
Here is what I have:

require ‘rubygems’  
#~ require ‘ruby-debug’

require ‘date’  
class Event \< DateTime  
class \<\< self  
alias :old\_new :new  
def new summary, due\_date, long\_text=’’, alarm\_time=0  
dt = DateTime.parse( due\_date )  
obj = old\_new(dt.year, dt.month, dt.day, dt.hour, dt.min, dt.sec,  
dt.sg)  
obj.send :initialize, summary, long\_text, alarm\_time  
obj  
end  
end

attr\_accessor :summary, :long\_text, :alarm\_time

def initialize summary, long\_text=’’, alarm\_time=0  
@summary = summary  
@long\_text = long\_text  
@alarm\_time = alarm\_time  
end  
end

e = Event.new “something to do”, “2009-10-10T00:00”  
puts e.summary  
puts e.year

e is an event at the end of the code, it has a summary and what ever  
else I pass in but the DateTime structures are not initialized  
correctly. In other words e.year gives me an error and here it is:

/usr/lib/ruby/1.8/date.rb:492:in `ajd_to_jd': undefined method`+’ for  
nil:NilClass (NoMethodError)  
from /usr/lib/ruby/1.8/date.rb:1051:in ` __21105__' from (eval):4:in`jd’  
from /usr/lib/ruby/1.8/date.rb:1066:in ` __21521__' from (eval):4:in`civil’  
from /usr/lib/ruby/1.8/date.rb:1081:in `year’  
from D:/workspace/todo/lib/tester.rb:29

I have tryed some other variants of the code as well and it gets even  
worse. Can any one help, please?  
Bye, Luis

---

<div class="post-metadata">

**Author:** ![luisealvarezb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/4bbf92/32.png) [@luisealvarezb](https://www.ruby-forum.com/u/luisealvarezb)\
**Post date:** [September 19, 2009, 12:23am UTC](https://www.ruby-forum.com/t/how-to-derive-from-datetime/175748/2 "2009-09-19T00:23:35Z")

</div>

.

On Fri, Sep 18, 2009 at 1:15 PM, luisealvarezb  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> #~ require ‘ruby-debug’  
> obj  
> end  
> nil:NilClass (NoMethodError)  
> from /usr/lib/ruby/1.8/date.rb:1051:in ` __21105__' from (eval):4:in `jd’  
> from /usr/lib/ruby/1.8/date.rb:1066:in ` __21521__' from (eval):4:in `civil’  
> from /usr/lib/ruby/1.8/date.rb:1081:in `year’  
> from D:/workspace/todo/lib/tester.rb:29
> 
> I have tryed some other variants of the code as well and it gets even  
> worse. Can any one help, please?

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.

---

<div class="post-metadata">

**Author:** ![luisealvarezb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/4bbf92/32.png) [@luisealvarezb](https://www.ruby-forum.com/u/luisealvarezb)\
**Post date:** [September 19, 2009, 2:20am UTC](https://www.ruby-forum.com/t/how-to-derive-from-datetime/175748/3 "2009-09-19T02:20:05Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![luisealvarezb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/4bbf92/32.png) [@luisealvarezb](https://www.ruby-forum.com/u/luisealvarezb)\
**Post date:** [September 19, 2009, 7:59am UTC](https://www.ruby-forum.com/t/how-to-derive-from-datetime/175748/4 "2009-09-19T07:59:34Z")

</div>

> 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)

---

<div class="post-metadata">

**Author:** ![luisealvarezb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/4bbf92/32.png) [@luisealvarezb](https://www.ruby-forum.com/u/luisealvarezb)\
**Post date:** [September 19, 2009, 10:25am UTC](https://www.ruby-forum.com/t/how-to-derive-from-datetime/175748/5 "2009-09-19T10:25:53Z")

</div>

On Sep 19, 12:58 am, Ehsanul H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def initialize(x)  
> obj = old\_new  
> end  
> 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.[restaurants - Search](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.

---

<div class="post-metadata">

**Author:** ![luisealvarezb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/4bbf92/32.png) [@luisealvarezb](https://www.ruby-forum.com/u/luisealvarezb)\
**Post date:** [September 19, 2009, 4:15am UTC](https://www.ruby-forum.com/t/how-to-derive-from-datetime/175748/6 "2009-09-19T04:15:59Z")

</div>

On Sep 18, 7:19 pm, Ehsanul H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def self.new( \*args )
> 
> 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.[restaurants - Search](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…
