Sha256: 4635ea80a777ba621aa962cb068ecc0ff512c3c74c1379b24d71a4838cee33ca

Contents?: true

Size: 853 Bytes

Versions: 8

Compression:

Stored size: 853 Bytes

Contents

module Stud
  # This implementation tries to keep clock more accurately.
  # Prior implementations still permitted skew, where as this one
  # will attempt to correct for skew.
  #
  # The execution patterns of this method should be that 
  # the start time of 'block.call' should always be at time T*interval
  def self.interval(time, &block)
    start = Time.now
    while true
      block.call
      duration = Time.now - start
      # Sleep only if the duration was less than the time interval
      if duration < time
        sleep(time - duration)
        start += time
      else
        # Duration exceeded interval time, reset the clock and do not sleep.
        start = Time.now
      end
    end # loop forever
  end # def interval
  
  def interval(time, &block)
    return Stud.interval(time, &block)
  end # def interval
end # module Stud

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
stud-0.0.17 lib/stud/interval.rb
stud-0.0.16 lib/stud/interval.rb
stud-0.0.15 lib/stud/interval.rb
stud-0.0.14 lib/stud/interval.rb
stud-0.0.13 lib/stud/interval.rb
stud-0.0.12 lib/stud/interval.rb
stud-0.0.11 lib/stud/interval.rb
stud-0.0.10 lib/stud/interval.rb