Sha256: d80cb62612e1e5b96db04a9410d96a0bd94580e42819236226e14ae5c2f6f082

Contents?: true

Size: 649 Bytes

Versions: 2

Compression:

Stored size: 649 Bytes

Contents

# Every Now And Then
module Enat
  # Yields to block every supplied interval
  #
  # @param [Fixnum, Float] interval_in_seconds minimum time interval between yields
  def every(interval_in_seconds)
    interval_in_seconds = interval_in_seconds.to_f

    unless interval_in_seconds > 0
      raise EnatError, "Interval must be non-zero"
    end

    iteration = 0

    loop do
      start_time = Time.now.to_f

      yield iteration
      iteration += 1

      time_elapsed = Time.now.to_f - start_time
      if time_elapsed >= interval_in_seconds
        next
      else
        sleep(interval_in_seconds - time_elapsed)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
enat-0.1.1 lib/enat/enat.rb
enat-0.1.0 lib/enat/enat.rb