Immediately print a string on stdout without new line?

J

Janus Bor

Hi everyone!

I'm trying to do something like this:

print "Processing... "

#some fancy code that takes ages, or maybe:
sleep(3)

print "done\n"


I was expecting to get the following output on my console:

Processing... [and after 3 secs:]done

However, the script first waits 3 seconds and then writes the whole line
at once. If I change the first line to

print "Processing...\n"

everything works as expected. But that's not really what I want. Is
there another way of doing this?

Thanks, Janus
 
F

fedzor

Hi everyone!

I'm trying to do something like this:

print "Processing... "

#some fancy code that takes ages, or maybe:
sleep(3)

print "done\n"


I was expecting to get the following output on my console:

Processing... [and after 3 secs:]done

However, the script first waits 3 seconds and then writes the whole
line
at once.

Try this:

print "Processing... "
STDOUT.flush

sleep 3

puts "done"

STDOUT#flush will ensure that everything in the buffer will be
printed, there and then


~ Ari
English is like a pseudo-random number generator - there are a
bajillion rules to it, but nobody cares.
 
G

Glen Holcomb

[Note: parts of this message were removed to make it a legal post.]

Hi everyone!

I'm trying to do something like this:

print "Processing... "

#some fancy code that takes ages, or maybe:
sleep(3)

print "done\n"


I was expecting to get the following output on my console:

Processing... [and after 3 secs:]done

However, the script first waits 3 seconds and then writes the whole line
at once. If I change the first line to

print "Processing...\n"

everything works as expected. But that's not really what I want. Is
there another way of doing this?

Thanks, Janus
Janus this works for me:

def processing
print "Processing... "
sleep(3)
print "done!"
end

processing
 
J

Janus Bor

fedzor said:
Try this:

print "Processing... "
STDOUT.flush

sleep 3

puts "done"

STDOUT#flush will ensure that everything in the buffer will be
printed, there and then

Thank you! That does the trick...


Glen said:
Janus this works for me:

def processing
print "Processing... "
sleep(3)
print "done!"
end

processing

Strange, on my machine (Ruby 1.86 on Linux) "Processing... " and "done!"
appear at the same time (after the 3 seconds).
 
G

Glen Holcomb

[Note: parts of this message were removed to make it a legal post.]

Thank you! That does the trick...




Strange, on my machine (Ruby 1.86 on Linux) "Processing... " and "done!"
appear at the same time (after the 3 seconds).
Until reading fedzor's post I had completely forgotten that different
Operating systems behave differently. I'm on a Windows machine here at
work. Glad you got it fixed though.
 
T

Tim Pease

Try this:

print "Processing... "
STDOUT.flush

sleep 3

puts "done"

STDOUT#flush will ensure that everything in the buffer will be
printed, there and then

Another option ...


STDOUT.sync = true
print "Processing... "


The sync flag will cause the IO stream to be flushed after every
write / print / puts.

Blessings,
TwP
 
D

Dave Bass

Another option: print your message to stderr, not stdout. Stderr is
usually not buffered by the OS (for exactly this sort of reason).

Dave
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top