DateTime is not a Date?

S

Sammy Larbi

So, I was playing around a little in Ruby and noticed that |DateTime| is
a subclass of |Date|, yet the method |today| is not defined for it.

I can certainly see how it doesn't make sense to have that method for
that class, but I'm wondering why a Date isn't a DateTime? I suppose
then we'd be removing the time element, so we'd get the same problem...
but it was just striking is all.
 
R

Robert Klemme

So, I was playing around a little in Ruby and noticed that |DateTime| is
a subclass of |Date|, yet the method |today| is not defined for it.

First of all this method is a singleton method of instance Date and
these are not inherited the same way as instance methods are.
DateTime.today does not make much sense - after all, what time would you
expect? Instead there is DateTime.now:
Date.methods - DateTime.methods => ["today"]
DateTime.methods - Date.methods
=> ["now", "valid_time?"]
I can certainly see how it doesn't make sense to have that method for
that class, but I'm wondering why a Date isn't a DateTime? I suppose
then we'd be removing the time element, so we'd get the same problem...
but it was just striking is all.

A Date cannot be a DateTime because it lacks time. Consequently
DateTime inherits Date:
Date.ancestors => [Date, Comparable, Object, Kernel]
DateTime.ancestors
=> [DateTime, Date, Comparable, Object, Kernel]

Once can certainly argue whether DateTime *is a* Date or rather *has a*
Date. But it is definitively clear that Date *is not* a DateTime simply
because it does not provide the same set of information that DateTime
provides (date *and* time).

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top