# Problem with date.succ

**URL:** <https://www.ruby-forum.com/t/problem-with-date-succ/65004>\
**Category:** Ruby\
**Created:** [August 15, 2006, 5:05pm UTC](https://www.ruby-forum.com/t/problem-with-date-succ/65004 "2006-08-15T17:05:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Russ\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/db5fbb/32.png) [@Russ\_P](https://www.ruby-forum.com/u/Russ_P)\
**Post date:** [August 15, 2006, 5:05pm UTC](https://www.ruby-forum.com/t/problem-with-date-succ/65004/1 "2006-08-15T17:05:49Z")

</div>

I’m having difficulty with a small program I’m writing dealing with  
dates. I’ve written the calendar class below. It works just fine  
when calling getMonth for all months EXCEPT NOVEMBER. Interrupting  
the call while in irb shows that it is busy in the reduce method of  
Rational, called from Date. Any ideas?

Thanks,  
Russ

require ‘date’  
class Calendar  
def initialize(year=Date.today.year)  
@year = year  
end

def getMonth(mo)  
dt = Date.new(@year, mo, 1)  
i = dt.month  
list = Array.new  
while i != (mo+1) % 12  
wk = getWeek(dt.month, dt.day)  
dt = wk[6].succ  
i = dt.month  
list.push(wk)  
end  
list  
end

def getWeek(mo, dy)  
dt = Date.new(@year, mo, dy)  
dt -= dt.wday  
list = Array.new  
for i in 0…6  
list.push dt  
dt = dt.succ  
end  
list  
end

def getDay(mo, dy)  
Date.new(@year, mo, dy)  
end  
end

---

<div class="post-metadata">

**Author:** ![Russ\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/db5fbb/32.png) [@Russ\_P](https://www.ruby-forum.com/u/Russ_P)\
**Post date:** [August 15, 2006, 11:36pm UTC](https://www.ruby-forum.com/t/problem-with-date-succ/65004/2 "2006-08-15T23:36:55Z")

</div>

> > ```
> > dt = Date.new(@year, mo, 1)
> > 
> > ```
> 
> next\_month = mo % 12 + 1  
> while i != next\_month
> 
> and at least the loop should work as you expect.
> 
> Regards,  
> Pit

Pit,  
Thank-you very much. Can’t believe I didn’t see that mistake.

Any idea why ruby’s date class needs to use rational numbers, though?  
Every time I’d get into the loop and break out in irb, I’d see a stack  
trace pointing to rational.rb…

Russ

---

<div class="post-metadata">

**Author:** ![Russ\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/db5fbb/32.png) [@Russ\_P](https://www.ruby-forum.com/u/Russ_P)\
**Post date:** [August 15, 2006, 11:00pm UTC](https://www.ruby-forum.com/t/problem-with-date-succ/65004/3 "2006-08-15T23:00:52Z")

</div>

Russ P. schrieb:

> ```
> i = dt.month
> 
> ```
> 
> (…)  
> Russ, for November, “mo” is 11, and “(mo+1) % 12” is zero, but  
> “dt.month” and “i” are always in the range (1…12), so you’ve got an  
> endless loop. Replace the line

while i != (mo+1) % 12

with

next\_month = mo % 12 + 1  
while i != next\_month

and at least the loop should work as you expect.

Regards,  
Pit

---

<div class="post-metadata">

**Author:** ![Russ\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/db5fbb/32.png) [@Russ\_P](https://www.ruby-forum.com/u/Russ_P)\
**Post date:** [August 16, 2006, 11:19am UTC](https://www.ruby-forum.com/t/problem-with-date-succ/65004/4 "2006-08-16T11:19:50Z")

</div>

On 8/15/06, Russ P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Any idea why ruby’s date class needs to use rational numbers, though?  
> Every time I’d get into the loop and break out in irb, I’d see a stack  
> trace pointing to rational.rb…

Because the Date is stored as Astronomical Julian Date number  
(whatever it is, see lib/date.rb comments and source) and the  
conversion somehow needs it. NB: Due to this, I obtained a decent  
speedup by caching Date object instead of creating them each time  
anew.
