# Sleep not adding up

**URL:** <https://www.ruby-forum.com/t/sleep-not-adding-up/126779>\
**Category:** Ruby\
**Created:** [January 22, 2008, 5:35am UTC](https://www.ruby-forum.com/t/sleep-not-adding-up/126779 "2008-01-22T05:35:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jbell38](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/edb3f5/32.png) [@jbell38](https://www.ruby-forum.com/u/jbell38)\
**Post date:** [January 22, 2008, 5:35am UTC](https://www.ruby-forum.com/t/sleep-not-adding-up/126779/1 "2008-01-22T05:35:35Z")

</div>

I was hoping that the following code would print out hi with one second  
gaps. However it waits 5 seconds and then prints out hi 5 times.

def test\_puts  
puts ‘hi’  
end

def test\_sleep  
sleep 1  
test\_puts  
end

5.downto(1) do  
test\_sleep  
end

---

<div class="post-metadata">

**Author:** ![jbell38](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/edb3f5/32.png) [@jbell38](https://www.ruby-forum.com/u/jbell38)\
**Post date:** [January 22, 2008, 5:47am UTC](https://www.ruby-forum.com/t/sleep-not-adding-up/126779/2 "2008-01-22T05:47:00Z")

</div>

Hi,

In message “Re: sleep not adding up”  
on Tue, 22 Jan 2008 13:35:29 +0900, Jeff B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
writes:

|I was hoping that the following code would print out hi with one second  
|gaps. However it waits 5 seconds and then prints out hi 5 times.

It works as you expected on my box (Linux). What’s your platform?

```
          matz.

```

---

<div class="post-metadata">

**Author:** ![jbell38](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/edb3f5/32.png) [@jbell38](https://www.ruby-forum.com/u/jbell38)\
**Post date:** [January 22, 2008, 5:53am UTC](https://www.ruby-forum.com/t/sleep-not-adding-up/126779/3 "2008-01-22T05:53:23Z")

</div>

Yukihiro M. wrote:

> Hi,
> 
> In message “Re: sleep not adding up”  
> on Tue, 22 Jan 2008 13:35:29 +0900, Jeff B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> writes:
> 
> |I was hoping that the following code would print out hi with one second  
> |gaps. However it waits 5 seconds and then prints out hi 5 times.
> 
> It works as you expected on my box (Linux). What’s your platform?
> 
> ```
> matz.
> 
> ```

Windows XP

---

<div class="post-metadata">

**Author:** ![jbell38](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/edb3f5/32.png) [@jbell38](https://www.ruby-forum.com/u/jbell38)\
**Post date:** [January 22, 2008, 7:55am UTC](https://www.ruby-forum.com/t/sleep-not-adding-up/126779/4 "2008-01-22T07:55:38Z")

</div>

Daniel S. wrote:

> On windows, you need to manually flush STDOUT/STDERR if you want to see  
> the output prior to the program ending.

Yes - or read some input from $stdin. A difference in the C library  
settings which might depend on which type of build you’re using.

Clifford H…

---

<div class="post-metadata">

**Author:** ![jbell38](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/edb3f5/32.png) [@jbell38](https://www.ruby-forum.com/u/jbell38)\
**Post date:** [January 22, 2008, 5:58am UTC](https://www.ruby-forum.com/t/sleep-not-adding-up/126779/5 "2008-01-22T05:58:06Z")

</div>

> def test\_puts  
> puts ‘hi’  
> STDOUT.flush  
> end
> 
> def test\_sleep  
> sleep 1  
> test\_puts  
> end
> 
> 5.downto(1) do  
> test\_sleep  
> end

On windows, you need to manually flush STDOUT/STDERR if you want to see  
the output prior to the program ending.

Dan.
