From: Todd Benson Date: 2009-07-14T02:27:40+09:00 Subject: Re: calculating an array of dates On Mon, Jul 13, 2009 at 10:42 AM, Eric Peterson wrote: > Siep Korteling wrote: > >> >> I have not looked into the reasons why your code doesn't work, just took >> your post as a coding exercise. My try looks like this: >> >> require 'Date' >> class Date >>   def thursdays_upto_now >>      end_date = Date.today >>      res = [] >>      # get the last thursday: >>      end_date -= 1 while (end_date.wday != 4) >>      end_date.step(self, -7) do |date| >>        res << date >>      end >>      res >>    end >>  end >> >> start_date = Date.parse( 'Thu Jun 11 13:14:44 -0700 2009' ) >> start_date.thursdays_upto_now.each{|thursday| puts thursday.to_s} # or >> whatever >> >> I was happy finding out Date has a step method. >> >> hth, I had some fun, >> >> Siep > > > much better then my final code.  I guess my mind is still stuck in > FORTRAN method of coding.  I'm finding Ruby very nice and interesting. > Lots of new tricks to learn. ... today = Date.today puts( today + 4 - today.wday) ...no need for the while with the -=. In fact won't the -= always give you the previous Thursday? The #step I didn't know about. Good advice. I didn't test the above code, but I'm pretty sure it will work for all edge cases. Todd