sleep not adding up

J

Jeff Bowen

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
 
J

Jeff Bowen

Yukihiro said:
Hi,

In message "Re: 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.

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

matz.

Windows XP
 
D

Daniel Sheppard

def test_puts
puts 'hi' STDOUT.flush
end
=20
def test_sleep
sleep 1
test_puts
end
=20
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.
 
C

Clifford Heath

Daniel said:
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 Heath.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,216
Latest member
topweb3twitterchannels

Latest Threads

Top