Caleb Clausen
5/26/2009 1:55:00 AM
On 5/25/09, Tom Ricks <carrottop123@gmail.com> wrote:
> Well, I'm not really sure. The intent is to have a repetitive code ran
> at quite a few different times with changing variables. The sleep option
> doesn't really seem reliable and realistic, although its the only thing
> so far. The code I can come up with is this:
>
> class TestClass
> def testtime(timetorun)
> timetorun = @timetorun
> sleep(1) while Time.now.strftime("%I:%M:%S %p") < ("#{@timetorun}")
> #Somewhere code to run when Time.now = timetorun
> end
> end
> test = TestClass.new
> test.testtime("11:04:40 AM")
>
> The problems are #1. I don't know how to link the time code to the code
> to be run when the time code is fulfilled.
I think procs are the feature you want to know about here.
> #2. If I have a list of times
> to be run, and one time is out of order, it will just sleep past the out
> of order time.
so, sort 'em.
> #3. While the code is waiting for a certain time, with
> sleep, it cannot do anything else.
Select, or threads, or some kind of event driven system like
EventMachine are the usual solutions to this problem. I forgot to
mention select before.
> Is there a way that I can run code
> that will watch for a time to be fulfilled on many levels instead of
> just waiting until a time, moving on and waiting for the next? Is it
> possible to do something like this:
>
> when test.testtime is true/finished?
> run some code
> end
>
Well, in general what you want here is a timer. It's very easy to
write your own; it sounds like you're close now, you just need to
learn about sort.
>> 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.
>
> How would I do this?
Hmm, you have some learning to do. Including how to find information
about the standard library. I suggest you make friends with ri, which
is the command-line tool that displays stdlib documentation. Google
can also be very helpful. You also need some general background on
ruby and programming concepts; I'd suggest the book 'programming
ruby'. There's a free version of an old edition online.