Forum: Ruby Immediately print a string on stdout without new line?

8309e758631e96bd92f5ec2f39eb2dce?d=identicon&s=25 Janus Bor (janus)
on 2008-07-15 23:14
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
Dec001d46d9ccb152be2f2c8a143934d?d=identicon&s=25 fedzor (Guest)
on 2008-07-15 23:22
(Received via mailing list)
On Jul 15, 2008, at 5:09 PM, Janus Bor wrote:

>
>
> 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.
9d4960f8319664f0f7896230eebace73?d=identicon&s=25 Glen Holcomb (Guest)
on 2008-07-15 23:23
(Received via mailing list)
On Tue, Jul 15, 2008 at 3:09 PM, Janus Bor <janus@urban-youth.com>
wrote:

>
> everything works as expected. But that's not really what I want. Is
> there another way of doing this?
>
> Thanks, Janus
> --
> Posted via http://www.ruby-forum.com/.
>
>
Janus this works for me:

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

processing

--
"Hey brother Christian with your high and mighty errand, Your actions
speak
so loud, I can't hear a word you're saying."

-Greg Graffin (Bad Religion)
8309e758631e96bd92f5ec2f39eb2dce?d=identicon&s=25 Janus Bor (janus)
on 2008-07-15 23:31
fedzor wrote:
> 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 Holcomb wrote:
> 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).
9d4960f8319664f0f7896230eebace73?d=identicon&s=25 Glen Holcomb (Guest)
on 2008-07-15 23:37
(Received via mailing list)
On Tue, Jul 15, 2008 at 3:26 PM, Janus Bor <janus@urban-youth.com>
wrote:

> > STDOUT#flush will ensure that everything in the buffer will be
> > print "Processing... "
> Posted via http://www.ruby-forum.com/.
>
>
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.

--
"Hey brother Christian with your high and mighty errand, Your actions
speak
so loud, I can't hear a word you're saying."

-Greg Graffin (Bad Religion)
4d5b5dd4e263d780a5dfe7ac8b8ac98c?d=identicon&s=25 Tim Pease (Guest)
on 2008-07-16 03:43
(Received via mailing list)
On Jul 15, 2008, at 3:17 PM, fedzor wrote:

> 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
9d751d4cd704a4efe3a335c77c4487a3?d=identicon&s=25 Dave Bass (dogsbody)
on 2008-07-16 13:56
Another option: print your message to stderr, not stdout. Stderr is
usually not buffered by the OS (for exactly this sort of reason).

Dave
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.