[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Fwd: Enumerating only months in a Date range?

John Lam

5/10/2005 2:43:00 PM

Is there a straight-forward way to list all of the months in a Date
range? If I construct a date range, the each method iterates by day
only.

Thanks
-John
http://www.iu...


6 Answers

Mark Hubbart

5/10/2005 3:18:00 PM

0

On 5/10/05, John Lam <drjflam@gmail.com> wrote:
> Is there a straight-forward way to list all of the months in a Date
> range? If I construct a date range, the each method iterates by day
> only.

There isn't a built-in way (that I know of), but it's pretty simple to
subclass date to make it work:

class MonthlyDate < Date
def succ
self >> 1
end
end

class Date
def to_monthly
m = MonthlyDate.new
instance_variables.map do |var|
m.instance_variable_set( var,instance_variable_get( var ) )
end
m
end
end

Then convert your Dates to MonthlyDates (using #to_monthly) before
creating your range. The range will now skip months rather than days.

HTH,
Mark


John Lam

5/10/2005 7:13:00 PM

0

Thanks, Mark!

The self >> 1 line is um, rather mysterious to me. How does that skip by months?

Cheers,
-John
http://www.iu...


Guillaume Marcais

5/10/2005 8:17:00 PM

0

On Wed, 2005-05-11 at 04:13 +0900, John Lam wrote:
> Thanks, Mark!
>
> The self >> 1 line is um, rather mysterious to me. How does that skip by months?

$ ri 'Date#>>'
---------------------------------------------------------------- Date#>>
>>(n)
------------------------------------------------------------------------
Return a new Date object that is +n+ months later than the current
one.

If the day-of-the-month of the current Date is greater than the
last day of the target month, the day-of-the-month of the returned
Date will be the last day of the target month.

Guillaume.


> Cheers,
> -John
> http://www.iu...
>



John Lam

5/10/2005 8:34:00 PM

0

> $ ri 'Date#>>'

Doh! Thanks Guillaume.

-John
http://www.iu...


Mark Hubbart

5/10/2005 8:38:00 PM

0

On 5/10/05, John Lam <drjflam@gmail.com> wrote:
> Thanks, Mark!
>
> The self >> 1 line is um, rather mysterious to me. How does that skip by months?

That's the bitshift operator, which is often used in classes for other
purposes. In the Date, it's used to add and subtract months:
Date.today << 2 will give you 2 months ago today, while Date.today >>
1 will give you next month today. I guess they decided to use it for
that because "+" and "-" were already taken, used for adding and
subtracting days.

Ranges use #succ (successor) to get the next object in a series. By
redefining #succ to return self >> 1 (self + 1 month), we get a range
of dates that skips by months rather than days.

cheers,
Mark


John Lam

5/10/2005 8:49:00 PM

0

The use of the bitshift operator surprised me. Crazy me, I was looking
for a next_month method :)

Thanks again for the insight!
-John
http://www.iu...