class RBlock::Timer
RBlock::Timer
is a simple Go inspired timer. Usage timer = RBlock::Timer.new(10)
timer.wait do
# ... someting once the time is up
end
Constants
- NAME
Public Class Methods
new(duration)
click to toggle source
Constructs and starts a timer @param duration [Numeric] duration in seconds
# File lib/rblocks/timer.rb, line 17 def initialize(duration) @ractor = Ractor.new(duration, name: NAME) do |time| sleep(time) Ractor.yield(:done) end end
Public Instance Methods
wait() { |self| ... }
click to toggle source
Blocks until the timer is done. Yields self to block if one is given
# File lib/rblocks/timer.rb, line 28 def wait done = @ractor.take yield(self) if block_given? done end