# Problem with date.succ

**URL:** <https://rubytalk.org/t/problem-with-date-succ/29993>\
**Category:** ruby-talk\
**Created:** [15 August 2006 15:03 UTC](https://rubytalk.org/t/problem-with-date-succ/29993 "2006-08-15T15:03:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Russ\_Pridemore](https://avatars.discourse-cdn.com/v4/letter/r/ecae2f/32.png) [@Russ\_Pridemore](https://rubytalk.org/u/Russ_Pridemore)\
**Post date:** [15 August 2006 15:03 UTC](https://rubytalk.org/t/problem-with-date-succ/29993/1 "2006-08-15T15:03: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  
&nbsp;&nbsp;&nbsp;def initialize(year=Date.today.year)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@year = year  
&nbsp;&nbsp;&nbsp;end

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

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

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

---

<div class="post-metadata">

**Author:** ![Pit](https://avatars.discourse-cdn.com/v4/letter/p/f14d63/32.png) [@Pit](https://rubytalk.org/u/Pit)\
**Post date:** [15 August 2006 20:57 UTC](https://rubytalk.org/t/problem-with-date-succ/29993/2 "2006-08-15T20:57:24Z")

</div>

Russ Pridemore schrieb:

> 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?
> 
> (...)
> 
> &nbsp;&nbsp;def getMonth(mo)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dt = Date.new(@year, mo, 1)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i = dt.month  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list = Array.new  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while i != (mo+1) % 12  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wk = getWeek(dt.month, dt.day)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dt = wk[6].succ  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i = dt.month  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list.push(wk)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list  
> &nbsp;&nbsp;end
> 
> (...)

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

&nbsp;&nbsp;&nbsp;while i != (mo+1) % 12

with

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

and at least the loop should work as you expect.

Regards,  
Pit

---

<div class="post-metadata">

**Author:** ![Russ\_Pridemore](https://avatars.discourse-cdn.com/v4/letter/r/ecae2f/32.png) [@Russ\_Pridemore](https://rubytalk.org/u/Russ_Pridemore)\
**Post date:** [15 August 2006 21:34 UTC](https://rubytalk.org/t/problem-with-date-succ/29993/3 "2006-08-15T21:34:11Z")

</div>

> Russ Pridemore schrieb:
> 
> > 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?
> > 
> > (...)
> > 
> > &nbsp;&nbsp;def getMonth(mo)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dt = Date.new(@year, mo, 1)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i = dt.month  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list = Array.new  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while i != (mo+1) % 12  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wk = getWeek(dt.month, dt.day)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dt = wk[6].succ  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i = dt.month  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list.push(wk)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list  
> > &nbsp;&nbsp;end
> > 
> > (...)
> 
> 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
> 
> &nbsp;&nbsp;&nbsp;while i != (mo+1) % 12
> 
> with
> 
> &nbsp;&nbsp;&nbsp;next\_month = mo % 12 + 1  
> &nbsp;&nbsp;&nbsp;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:** ![Jan\_Svitok](https://avatars.discourse-cdn.com/v4/letter/j/cab0a1/32.png) [@Jan\_Svitok](https://rubytalk.org/u/Jan_Svitok)\
**Post date:** [16 August 2006 09:18 UTC](https://rubytalk.org/t/problem-with-date-succ/29993/4 "2006-08-16T09:18:20Z")

</div>

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.

> **···**
>
> On 8/15/06, Russ Pridemore \<russ.pridemore@gmail.com\> 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...
