Class: TimeIterator

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/time_iterator.rb,
lib/time_iterator/version.rb

Overview

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

Constant Summary collapse

VERSION =
"0.1.1"

Instance Method Summary collapse

Constructor Details

#initialize(start, stop, step = 1) ⇒ TimeIterator

Returns a new instance of TimeIterator.

Parameters:

  • start (Time)
  • stop (Time)
  • step (#to_f) (defaults to: 1)


14
15
16
17
18
# 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

Instance Method Details

#each {|time| ... } ⇒ TimeIterator

Iterates through time range.

Yields:

  • (time)

Returns:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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) ⇒ TimeIterator

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

Parameters:

  • step (#to_f)

Returns:



25
26
27
# File 'lib/time_iterator.rb', line 25

def step step
  self.class.new @start, @stop, step
end