# Sleep for lesser of two values

**URL:** <https://www.ruby-forum.com/t/sleep-for-lesser-of-two-values/115826>\
**Category:** Ruby\
**Created:** [September 28, 2007, 9:07pm UTC](https://www.ruby-forum.com/t/sleep-for-lesser-of-two-values/115826 "2007-09-28T21:07:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![eclubb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ecc23a/32.png) [@eclubb](https://www.ruby-forum.com/u/eclubb)\
**Post date:** [September 28, 2007, 9:07pm UTC](https://www.ruby-forum.com/t/sleep-for-lesser-of-two-values/115826/1 "2007-09-28T21:07:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![eclubb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ecc23a/32.png) [@eclubb](https://www.ruby-forum.com/u/eclubb)\
**Post date:** [September 28, 2007, 9:16pm UTC](https://www.ruby-forum.com/t/sleep-for-lesser-of-two-values/115826/2 "2007-09-28T21:16:45Z")

</div>

On 28/09/2007, Earle C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![eclubb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ecc23a/32.png) [@eclubb](https://www.ruby-forum.com/u/eclubb)\
**Post date:** [September 28, 2007, 9:19pm UTC](https://www.ruby-forum.com/t/sleep-for-lesser-of-two-values/115826/3 "2007-09-28T21:19:06Z")

</div>

Earle C. wrote:

> Thanks,  
> Earle

sleep([15, end\_time -t].min)

---

<div class="post-metadata">

**Author:** ![eclubb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ecc23a/32.png) [@eclubb](https://www.ruby-forum.com/u/eclubb)\
**Post date:** [September 28, 2007, 9:20pm UTC](https://www.ruby-forum.com/t/sleep-for-lesser-of-two-values/115826/4 "2007-09-28T21:20:51Z")

</div>

On Sat, 29 Sep 2007 04:07:40 +0900, Earle C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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
