<< : can some body check this code??

C

Chinna Karuppan

why is it not working properly( weekly calendar)...
require "Date"
def mon_days(month,year=Date.today.year)
mday = [nil,31,28,31,30,31,30,31,31,30,31,30,31]
mday[2] = 29 if Date.leap? year
mday[month]
end

def cal_week(month,year=Date.today.year)
days = 1..mon_days(month,year)
weeks = []
week = []
wday = 1
for i in days
wday = Date.new(year,month,i).wday
week[wday] = i
if wday == 6
weeks << week
p week,weeks
week.clear

end
end
weeks << week if week.length != 0
weeks
end

p cal_week(4)

I think it is the problem of week still remembering the values....
THnks
CHinna
 
C

Chinna Karuppan

I think I found an answer not sure if it is normal way to do things....
weeks << week.clone instead of week...
Looking forward for comments...
THnks
 
T

Todd Benson

I think I found an answer not sure if it is normal way to do things....
weeks << week.clone instead of week...
Looking forward for comments...
THnks

Yes, you are using the same Array object over and over with
week.clear. I tend to just do week = [] instead of clone.

Todd
 
T

Todd Benson

I think I found an answer not sure if it is normal way to do things....
weeks << week.clone instead of week...
Looking forward for comments...
THnks

Yes, you are using the same Array object over and over with
week.clear. I tend to just do week = [] instead of clone.

Todd

Sorry, didn't post example. Here it is using your code...

require "Date"
def mon_days(month,year=Date.today.year)
mday = [nil,31,28,31,30,31,30,31,31,30,31,30,31]
mday[2] = 29 if Date.leap? year
mday[month]
end

def cal_week(month,year=Date.today.year)
days = 1..mon_days(month,year)
weeks = []
week = []
wday = 1
for i in days
wday = Date.new(year,month,i).wday
week[wday] = i
if wday == 6
weeks << week
week = []
end
end
weeks << week if week.length != 0
weeks
end

p cal_week(4)

Todd
 
R

Rolando Abarca

why is it not working properly( weekly calendar)...
require "Date"
def mon_days(month,year=Date.today.year)
mday = [nil,31,28,31,30,31,30,31,31,30,31,30,31]
mday[2] = 29 if Date.leap? year
mday[month]
end

def cal_week(month,year=Date.today.year)
days = 1..mon_days(month,year)
weeks = []
week = []
wday = 1
for i in days
wday = Date.new(year,month,i).wday
week[wday] = i
if wday == 6
weeks << week
p week,weeks
week.clear

end
end
weeks << week if week.length != 0
weeks
end

p cal_week(4)

I think it is the problem of week still remembering the values....

the problem is that you're using the same week array. Here's another
solution for the same problem:

def cal_week(month, year = Date.today.year)
fotm = Date.new(year,month,1) # first of the month
lotm = (fotm >> 1) - 1 # last of the month
weeks = []
week = []
fotm.upto(lotm) do |d|
week[d.wday] = d.day
if d.wday == 6
weeks << week
week = []
end
end
weeks << week unless week.empty?
end

p cal_week(4)
THnks
CHinna


regards,
 
C

Chinna Karuppan

Thanks Todd, Rolando . I got the point (week[])....
I appreciate your help and time.
THnks
 

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

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top