class TimeIterator

Wraper class that allows easily iterate through the times with given step size (1 second by default).

Constants

VERSION

Public Class Methods

new(start, stop, step = 1) click to toggle source

@param [Time] start @param [Time] stop @param [#to_f] step

# File lib/time_iterator.rb, line 14
def initialize start, stop, step = 1
  @start, @stop, @step = start, stop, step.to_f

  raise "Iteration step should be higher than zero." if 0.00 > @step
end

Public Instance Methods

each() { |curr| ... } click to toggle source

Iterates through time range.

@yield [time] @return [TimeIterator]

# File lib/time_iterator.rb, line 34
def each &block
  curr = @start + 0

  if @start < @stop
    while curr <= @stop
      yield curr
      curr += @step
    end
  else
    while curr >= @stop
      yield curr
      curr -= @step
    end
  end

  self
end
step(step) click to toggle source

Returns new iterator with same start/stop values but new step size.

@param [#to_f] step @return [TimeIterator]

# File lib/time_iterator.rb, line 25
def step step
  self.class.new @start, @stop, step
end