Problem with date.succ

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

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?

(...)

  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

(...)

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

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?

(...)

  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

(...)

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

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

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...