Class: Alien::AlienBlockTimer

Inherits:
Object
  • Object
show all
Defined in:
lib/alien/alienblocktimer.rb

Instance Method Summary collapse

Constructor Details

#initialize(description = "", &block) ⇒ AlienBlockTimer

Setup the timer and pass in an optional description. If there is an associated block with the call, execute a timing measurment.



70
71
72
73
74
75
76
# File 'lib/alien/alienblocktimer.rb', line 70

def initialize (description="",&block)
  @t1=Time.now
  @measurement = "Timer initialized."
  unless block.nil?
    measure(description){yield}
  end
end

Instance Method Details

#elapsedObject

The time since we called start or since the class was initialized.



100
101
102
# File 'lib/alien/alienblocktimer.rb', line 100

def elapsed
  Time.now - @t1
end

#inspectObject



104
105
106
# File 'lib/alien/alienblocktimer.rb', line 104

def inspect
  @measurement
end

#measure(description = "", &block) ⇒ Object

Measure the execution time for an asscociated code block.



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/alien/alienblocktimer.rb', line 80

def measure(description="", &block)

  start

  unless block.nil?
    yield
    @measurement =  description + elapsed.to_s
  else
    #Error message Haiku. :)
    @measurement = "No block to measure. Your timer waits patiently. Give him one to use."
  end

end

#startObject

Grab a start time



95
96
97
# File 'lib/alien/alienblocktimer.rb', line 95

def start
  @t1=Time.now
end

#to_sObject



108
109
110
# File 'lib/alien/alienblocktimer.rb', line 108

def to_s
  @measurement
end