Date#- => Rational ?

ruby-1.9.2-p136 :080 > Rails.version

ruby-1.9.2-p136 :081 > ( -
Rational < Numeric

Why is the difference Rational ?

Robert Pankowecki

This is actually a feature of the built-in date library; I get the same result on 1.8 in vanilla irb (after doing "require 'date'"). It's intended (AFAIK) to enable sensible operations between Date and DateTime objects, since subtracting Dates gives a value which should be read as "number of days" and DateTimes can mix fractions in:

require 'date'
d = Date.civil(2011,4,6)
d2 = DateTime.civil(2011,4,6,12,0,0)

d2 - d # returns Rational(1,2) - in other words, "half a day" since the docs say Date objects are considered to have a time of midnight

--Matt Jones

Thanks for the information.

I got
NoMethodError: undefined method `-' for #<Date:0x97829f0>
when trying to subtract two dates so I thought that the operator is
defined in Rails.

Apparently it is not. " require 'date' "must be called to have it.
Not very intuitive.

Robert Pankowecki