problem with date.succ

R

Russ Pridemore

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
 
P

Pit Capitain

Russ said:
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
 
R

Russ Pridemore

Russ said:
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
 
J

Jan Svitok

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top