So I have two ruby Date objects, and I want to iterate them every month. For example if I have Date.new(2008, 12) and Date.new(2009, 3), it would yield me 2008-12, 2009-1, 2009-
As a helper method:
def iterate(d1, d2)
date = d1
while date <= d2
yield date
date = date >> 1
end
end
Usage:
start_date = Date.new(2008, 12)
end_date = Date.new(2009, 3)
iterate(start_date, end_date){|date| puts date}
Or, if you prefer to monkey patch Date:
class Date
def upto(end_date)
date = self
while date <= end_date
yield date
date = date >> 1
end
end
end
Usage:
start_date = Date.new(2008, 12)
end_date = Date.new(2009, 3)
start_date.upto(end_date){|date| puts date}