Sleep not adding up

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

Hi,

In message “Re: sleep not adding up”
on Tue, 22 Jan 2008 13:35:29 +0900, Jeff B. [email protected]
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.

Yukihiro M. wrote:

Hi,

In message “Re: sleep not adding up”
on Tue, 22 Jan 2008 13:35:29 +0900, Jeff B. [email protected]
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

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…

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.