Iterate over Ruby Time object with delta -


Is there a way to recycle the time range in Ruby, and the delta is set?

Here's what I would like to do:

  for hours (start_time..end_time, hour) hour # = & gt; Time object is set to the end of the hour  

You can iterate on the time objects, but it gives the second return between the two seconds. Do I really need a way to set offset or delta (like Minutes, Hours, etc.)

Is it made in Ruby, or is there any decent plug-in available? You can use range # step before <1.9>

1.9

  (start_time..end_time) .step (3600). Hours. | # ... and  

However, this strategy is quite slow because it will call time # succ 3600 times. Instead, as described by Dolenco, a more efficient solution is to use a simple loop:

  hour = start_time while hour < End_time # ... hour + = 3600 end  

If you are using rail, then you can replace 3600 with 1.hour , which It is quite readable.


Comments