On May 25, 2009, at 13:36, Caleb Clausen wrote: > On 5/25/09, Tom Ricks <carrottop123 / gmail.com> wrote: >> I am new to ruby and I am attempting to run some code at a certain >> time. The Ruby Time class is very interesting, you can add time, >> and do >> all of these amazing things but i cannot find a way to run code at a >> certain time. I feel like I'm missing something. So far all I have >> is: >> >> sleep(1) while Time.now < ("Time") >> >> How would I link some code to be run to this? Or is there an easier >> way? > > The proper answer depends on the context where this code will be used. > Do you want your whole program to stop until some target time? In that > case, something as simple as > > sleep(target_time-Time.now) > code_to_be_delayed > > will do the trick nicely. Note that #sleep won't necessarily sleep as long as you like (second sentence): $ ri Kernel#sleep ----------------------------------------------------------- Kernel#sleep sleep([duration]) => fixnum From Ruby 1.8 ------------------------------------------------------------------------ Suspends the current thread for duration seconds (which may be any number, including a Float with fractional seconds). Returns the actual number of seconds slept (rounded), which may be less than that asked for if another thread calls Thread#run. Zero arguments causes sleep to sleep forever. [...]