I borrowed some code from a site, but I don't know how to get it to display.
class Stopwatch
def start
@accumulated = 0 unless @accumulated
@elapsed = 0
@start = Time.now
@mybutton.configure('text' => 'Stop')
@mybutton.command { stop }
@timer.start
end
def stop
@mybutton.configure('text' => 'Start')
@mybutton.command { start }
@timer.stop
@accumulated += @elapsed
end
def reset
stop
@accumulated, @elapsed = 0, 0
@mylabel.configure('text' => '00:00:00.00.000')
end
def tick
@elapsed = Time.now - @start
time = @accumulated + @elapsed
h = sprintf('%02i', (time.to_i / 3600))
m = sprintf('%02i', ((time.to_i % 3600) / 60))
s = sprintf('%02i', (time.to_i % 60))
mt = sprintf('%02i', ((time - time.to_i)*100).to_i)
ms = sprintf('%04i', ((time - time.to_i)*10000).to_i)
ms[0..0]=''
newtime = "#{h}:#{m}:#{s}.#{mt}.#{ms}"
@mylabel.configure('text' => newtime)
end
end
How would I go about getting this running? Thanks
Based upon the additional code rkneufeld posted, this class requires a timer that is specific to Tk. To do it on the console, you could just create a loop that calls tick over and over. Of course, you have to remove all the code that was related to the GUI:
class Stopwatch
def start
@accumulated = 0 unless @accumulated
@elapsed = 0
@start = Time.now
# @mybutton.configure('text' => 'Stop')
# @mybutton.command { stop }
# @timer.start
end
def stop
# @mybutton.configure('text' => 'Start')
# @mybutton.command { start }
# @timer.stop
@accumulated += @elapsed
end
def reset
stop
@accumulated, @elapsed = 0, 0
# @mylabel.configure('text' => '00:00:00.00.000')
end
def tick
@elapsed = Time.now - @start
time = @accumulated + @elapsed
h = sprintf('%02i', (time.to_i / 3600))
m = sprintf('%02i', ((time.to_i % 3600) / 60))
s = sprintf('%02i', (time.to_i % 60))
mt = sprintf('%02i', ((time - time.to_i)*100).to_i)
ms = sprintf('%04i', ((time - time.to_i)*10000).to_i)
ms[0..0]=''
newtime = "#{h}:#{m}:#{s}.#{mt}.#{ms}"
# @mylabel.configure('text' => newtime)
end
end
watch = Stopwatch.new
watch.start
1000000.times do
puts watch.tick
end
You'll end up with output like this:
00:00:00.00.000
00:00:00.00.000
00:00:00.00.000
...
00:00:00.00.000
00:00:00.00.000
00:00:00.01.160
00:00:00.01.160
...
Not particularly useful, but there it is. Now, if you're looking to do something similar in Shoes, try this tutorial that is very similar.