Class: Ludy::Timer

Inherits:
Object
  • Object
show all
Defined in:
lib/ludy/timer.rb

Overview

simple and stupid timer for puzzle_generator…

Instance Method Summary collapse

Constructor Details

#initialize(timeout) ⇒ Timer

create a timer for timeout seconds.



8
9
10
# File 'lib/ludy/timer.rb', line 8

def initialize timeout
  @timeout = timeout
end

Instance Method Details

#startObject

start ticking



12
13
14
15
16
17
18
19
20
# File 'lib/ludy/timer.rb', line 12

def start
  @now = Time.now
  @parent = Thread.current
  @thread = Thread.new{
    sleep @timeout
    @parent.raise Timeout.new("timeout for #{@timeout} seconds.")
  }
  self
end

#stopObject

stop ticking



26
27
28
# File 'lib/ludy/timer.rb', line 26

def stop
  @thread.kill
end

#total_timeObject

total time until start ticking



22
23
24
# File 'lib/ludy/timer.rb', line 22

def total_time
  Time.now - @now
end