42

I need to start from, for example, January 1 2013, and "do some things" for each date, resulting in a JSON file for each date.

I have the "do some things" part worked out for a single date, but I'm having a hard time starting at a date and looping through to another end date.

tadman
  • 208,517
  • 23
  • 234
  • 262
calf
  • 861
  • 2
  • 11
  • 23

2 Answers2

96

You can use ranges :

(Date.new(2012, 01, 01)..Date.new(2012, 01, 30)).each do |date|
  # Do stuff with date
end

or (see @awendt answer)

Date.new(2012, 01, 01).upto(Date.new(2012, 01, 30)) do |date|
  # Do stuff with date
end
Intrepidd
  • 19,772
  • 6
  • 55
  • 63
31

You could use:

 first.upto(last) do |date|

where first and last are Date objects.

See what I did here in a project of mine, for example.

awendt
  • 13,195
  • 5
  • 48
  • 66