how to put timer on ruby

G

griffith Khana

i need a timer for the program i am writing but i don't know the code so
please someone help me to write timer.......
thanx for your help...
 
K

Kaspar Schiess

i need a timer for the program i am writing but i don't know the code so
please someone help me to write timer.......
thanx for your help...

a) timeout:

require 'timeout'
timeout(4) do
# code in here will only run for 4 seconds
end

If you look at the implementation of timeout, you will see that it is
easy to time code using Kernel.sleep. This is however very imprecise and
can't be used for animation loops, for example.

b) EventMachine

require 'eventmachine'
time = Time.now
EventMachine::add_timer(1) do
puts "hello one second from #{time}!"
end

Eventmachine should allow you to do more precise timing. Your
possibilities are of course limited by the system you run on.

greetings,
kaspar
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top