ruby - why does subtraction between dates return a Rational type? -
i trying perform subtraction operation on dates.
date_sent = date.parse("2013-01-01") #=> tue, 01 jan 2013 date_now = date.today #=> wed, 04 sep 2013 days = (date_now - date_sent) #=> (246/1)
why date_now - date_sent
return rational
type?
it's expected behavior. docs:
d - other → date or rational
date.new(2001,2,3) - 1 #=> #<date: 2001-02-02 ...> date.new(2001,2,3) - date.new(2001) #=> (33/1)
a rational type used because can express difference exactly:
diff = datetime.new(2001,2,3) - datetime.new(2001,2,2,1) #=> (23/24)
whereas float can't:
diff.to_f #=> 0.9583333333333334
Comments
Post a Comment