[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

sleep for lesser of two values

Earle Clubb

9/28/2007 7:08:00 PM

I have a loop that runs for a variable amount of time, as defined by
@duration. I need to re-execute the command every 15 seconds for the
length of @duration, which is a float and can have any value. Can
anyone think of a more elegant way to do this than the code below?

end_time = Time.now + @duration
while (t = Time.now) < end_time
# execute command
sleep((15 < (end_time - t)) ? 15 : (end_time - t))
end


Thanks,
Earle
--
Posted via http://www.ruby-....

3 Answers

Pete Elmore

9/28/2007 7:16:00 PM

0

On 28/09/2007, Earle Clubb <eclubb@valcom.com> wrote:
> I have a loop that runs for a variable amount of time, as defined by
> @duration. I need to re-execute the command every 15 seconds for the
> length of @duration, which is a float and can have any value. Can
> anyone think of a more elegant way to do this than the code below?
>
> end_time = Time.now + @duration
> while (t = Time.now) < end_time
> # execute command
> sleep((15 < (end_time - t)) ? 15 : (end_time - t))
> end
I don't know about elegant, but it's a little more easy to read
sleep [15, end_time - t].min
than the ?: business.

Tim Hunter

9/28/2007 7:19:00 PM

0

Earle Clubb wrote:
> I have a loop that runs for a variable amount of time, as defined by
> @duration. I need to re-execute the command every 15 seconds for the
> length of @duration, which is a float and can have any value. Can
> anyone think of a more elegant way to do this than the code below?
>
> end_time = Time.now + @duration
> while (t = Time.now) < end_time
> # execute command
> sleep((15 < (end_time - t)) ? 15 : (end_time - t))
> end
>
>
> Thanks,
> Earle


sleep([15, end_time -t].min)

--
RMagick OS X Installer [http://rubyforge.org/project...]
RMagick Hints & Tips [http://rubyforge.org/forum/forum.php?for...]
RMagick Installation FAQ [http://rmagick.rubyforge.org/instal...]

MenTaLguY

9/28/2007 7:19:00 PM

0

On Sat, 29 Sep 2007 04:07:40 +0900, Earle Clubb <eclubb@valcom.com> wrote:
> I have a loop that runs for a variable amount of time, as defined by
> @duration. I need to re-execute the command every 15 seconds for the
> length of @duration, which is a float and can have any value. Can
> anyone think of a more elegant way to do this than the code below?
>
> end_time = Time.now + @duration
> while (t = Time.now) < end_time
> # execute command
> sleep((15 < (end_time - t)) ? 15 : (end_time - t))
> end

Maybe:

sleep [ 15, end_time - t ].min

-mental