Dave
Dave

Reputation: 11862

Rails: Loop over every hour between two times

If I define the following:

    timetogoback = Time.zone.local(2012,8,01,0,0)

    timeupto = Time.zone.local(2012,9,12,12,0)

As two times, how do I then create a loop that iterates over every hour between those times?

I am trying to run a once off task to create by-hour summary tables from existing data. So I want to loop and set the created_at to the correct hour so that the data calculated and saved is correct for that exact hour.

Thanks.

Upvotes: 1

Views: 901

Answers (1)

Trevor
Trevor

Reputation: 6689

Here is a while loop that increments timetogoback until it is greater than timeupto

while timeupto > timetogoback do
  timetogoback += 1.hour
  print "#{timetogoback}\n"
end

Upvotes: 1

Related Questions