From: Todd Benson Date: 2008-03-04T05:57:26+09:00 Subject: Re: making a monthly calendar... On Mon, Mar 3, 2008 at 2:43 PM, Todd Benson wrote: > > On Mon, Mar 3, 2008 at 2:14 PM, Mikkel Bruun wrote: > 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 No, actually the same code works, you just have to turn the -1..1 range into a loop for 12, which can be done like this... your_number % 12 + 1 so code would be... require 'date' today = Date.new(2008, 1, 8)#use whatever date you want offset = Date.new(today.year, today.month, 1).wday months_of_concern = (-1..1).map.inject([]) {|arr, i| arr << (today.month + i)} months_of_concern.map! {|i| i % 12 + 1} month_ranges = months_of_concern.map {|month| 1..Date.new( today.year, month, -1 ).day} day_sets = month_ranges.map {|i| i.to_a} offset.times { day_sets[1].unshift day_sets[0].pop } p day_sets[1..2].flatten.slice(0..42) Todd