Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby sleep or delay less than a second?

Tags:

ruby

People also ask

How does sleep work in Ruby?

Ruby sleep() methodsleep() method accepts number of seconds as argument and suspends the thread of execution for the amount of seconds being passed as parameter. The parameter can be passed as number or float or fraction. If you are working with Ruby on Rails, then you get the facility to pass parameter with .

How do you sleep a thread in Ruby?

Ruby provides a few ways to support scheduling threads in your program. The first way is by using the class method ::stop , to put the current running thread to sleep and schedule the execution of another thread.


sleep(1.0/24.0)

As to your follow up question if that's the best way: No, you could get not-so-smooth framerates because the rendering of each frame might not take the same amount of time.

You could try one of these solutions:

  • Use a timer which fires 24 times a second with the drawing code.
  • Create as many frames as possible, create the motion based on the time passed, not per frame.

Pass a float to sleep, like:

sleep 0.1