From: Caleb Clausen Date: 2009-05-26T05:36:42+09:00 Subject: Re: Runnin code at a certain time? On 5/25/09, Tom Ricks wrote: > Hello all, > 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. But if this code runs within the context of some larger system, which cannot afford to have the whole program be unable to respond to events for many seconds in a row (rails, for example), then sleep is a bad idea. In some cases, you might be able to put your sleep and code_to_be_delayed in another thread, but then you run into the problem of thread synchronization. If neither of my simple suggestions does the trick, you should post more details abut your application. (Maybe then someone will have enough information to make a specific suggestion... I doubt I will.)