# Immediately print a string on stdout without new line?

**URL:** <https://www.ruby-forum.com/t/immediately-print-a-string-on-stdout-without-new-line/143169>\
**Category:** Ruby\
**Created:** [July 15, 2008, 11:14pm UTC](https://www.ruby-forum.com/t/immediately-print-a-string-on-stdout-without-new-line/143169 "2008-07-15T23:14:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![janus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c2a13f/32.png) [@janus](https://www.ruby-forum.com/u/janus)\
**Post date:** [July 15, 2008, 11:14pm UTC](https://www.ruby-forum.com/t/immediately-print-a-string-on-stdout-without-new-line/143169/1 "2008-07-15T23:14:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![janus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c2a13f/32.png) [@janus](https://www.ruby-forum.com/u/janus)\
**Post date:** [July 15, 2008, 11:22pm UTC](https://www.ruby-forum.com/t/immediately-print-a-string-on-stdout-without-new-line/143169/2 "2008-07-15T23:22:41Z")

</div>

On Jul 15, 2008, at 5:09 PM, Janus B. 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.

---

<div class="post-metadata">

**Author:** ![janus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c2a13f/32.png) [@janus](https://www.ruby-forum.com/u/janus)\
**Post date:** [July 15, 2008, 11:23pm UTC](https://www.ruby-forum.com/t/immediately-print-a-string-on-stdout-without-new-line/143169/3 "2008-07-15T23:23:16Z")

</div>

On Tue, Jul 15, 2008 at 3:09 PM, Janus B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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/](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)

---

<div class="post-metadata">

**Author:** ![janus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c2a13f/32.png) [@janus](https://www.ruby-forum.com/u/janus)\
**Post date:** [July 15, 2008, 11:31pm UTC](https://www.ruby-forum.com/t/immediately-print-a-string-on-stdout-without-new-line/143169/4 "2008-07-15T23:31:03Z")

</div>

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 H. 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).

---

<div class="post-metadata">

**Author:** ![janus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c2a13f/32.png) [@janus](https://www.ruby-forum.com/u/janus)\
**Post date:** [July 16, 2008, 3:43am UTC](https://www.ruby-forum.com/t/immediately-print-a-string-on-stdout-without-new-line/143169/5 "2008-07-16T03:43:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![janus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c2a13f/32.png) [@janus](https://www.ruby-forum.com/u/janus)\
**Post date:** [July 16, 2008, 1:56pm UTC](https://www.ruby-forum.com/t/immediately-print-a-string-on-stdout-without-new-line/143169/6 "2008-07-16T13:56:54Z")

</div>

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

Dave

---

<div class="post-metadata">

**Author:** ![janus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c2a13f/32.png) [@janus](https://www.ruby-forum.com/u/janus)\
**Post date:** [July 15, 2008, 11:37pm UTC](https://www.ruby-forum.com/t/immediately-print-a-string-on-stdout-without-new-line/143169/7 "2008-07-15T23:37:18Z")

</div>

On Tue, Jul 15, 2008 at 3:26 PM, Janus B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

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