Date::Delta

I ran across a pretty non-intuitive (IMHO) behavior with Date::Delta
Objects.

irb(main):001:0> require ‘date/delta’
=> true
irb(main):002:0> d = Date.today - Date.today + Date::Delta.months(1)
=> ((0/1)+(1/1)*i)
irb(main):003:0> d.class
=> Complex

Okay, I figured out where this came from:
Date::Delta is implemented via the linear map

1 day -> 1
1 month -> i

from the timedeltas to the complex numbers.
It’s because the length of one month is different depending on where it
is added.
The multiplication of complex numbers is irrelevant here.

So isn’t it strange that this implementation detail comes up when doing
arithmetic with times? Would it be better if #<Date::Delta: +(0d
00:00’00"000)+(0y 1m) (0/1+1/1i)>
were returned?

What does everyone else think?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs