From: Todd Benson Date: 2008-03-04T08:10:15+09:00 Subject: Re: making a monthly calendar... On Mon, Mar 3, 2008 at 4:52 PM, Siep Korteling wrote: > > Todd Benson wrote: > > On Mon, Mar 3, 2008 at 2:37 PM, Jeremy McAnally > > wrote: > >> There's a calendar_helper plugin if this is for a Rails app. Works well for me. > >> > >> --Jeremy > > > > I agree. If this is to be productive, then you probably don't have to > > reinvent the wheel :) > > > > Todd > > Yes. Here is my effort anyway: > > require 'date' > require 'enumerator' > > def days_to_show(year, month, num_week_rows) > # returns an array of the dates in month, > # with the last days of the preceding month > # and the first days of the next month > > first_of_month = Date.new(year, month, 1) > first = first_of_month - (first_of_month.wday)+1 > # +1 for weeks starting on monday, remove for weeks starting on sunday > last = first + (num_week_rows * 7) > (first...last).to_a > end > > days = days_to_show(2008, 3, 6) > days.each_slice(7) do |week| > print "| " > week.each{|day| print day.to_s + "\t| "} > puts > end > > The only thing noteworthy is the range of dates, which produces an > array. > > Regards, > > Siep Mikkel, this is excellent use of the Date#- and Date#+ methods, and a much better approach. I still think my solution is more fun :) Todd