From: Todd Benson Date: 2008-03-04T05:42:56+09:00 Subject: Re: making a monthly calendar... On Mon, Mar 3, 2008 at 2:14 PM, Mikkel Bruun wrote: > Todd Benson wrote: > > On Mon, Mar 3, 2008 at 1:02 PM, Todd Benson wrote: > > >> > offset.upto(days.length+offset){boxes[offset+i]=days[i];i=i+1} > > >> > boxes array into parts of 7 > > >> with the correct number of days in each, centered around the current > > >> display, but, obviously, you won't have to do that, because you have > >> > > >> today = Date.today #use whatever date you want > >> > >> offset = Date.new(today.year, today.month, 1).wday > >> # ^^^^^^ that was the line I screwed up at first > >> months_of_concern = (-1..1).inject([]) {|a, i| a << (today.month + i)} > >> > >> month_ranges = months_of_concern.map {|month| 1..Date.new( today.year, > >> month, -1 ).day} > > > > That's one line and suppose to be today.month following today.year. > > > >> day_sets = month_ranges.map {|i| i.map} > >> > >> offset.times {day_sets[1].unshift day_sets[0].pop} > >> p day_sets[1..2].flatten.slice(0..42) > >> > > > > Todd > > thanks for all your help > > but your code seems to break when changing years. showing january 2008 > breaks when populating decemeber 2007 etc... > > i believe this line has to take this into account: > > > month_ranges = months_of_concern.map {|month| 1..Date.new( today.year, > month, -1 ).day} > > > Mikkel Yeah, I just realized the edge cases of January and December fail. I thought the Date object covered that with a -1 or 13 month. I'm looking into it right now. You might be better off subtracting a week and adding a week for your big array. For example... puts Date.new(2008, 1, 5) - 7 => 2007-12-29 Todd