---
title: "$stdout.sync = true to flush output immediately"
description:
  "Since IO operations are expensive IO results are not flushed immediately. See
  the technique presented here to flush output immediately."
canonical_url: "https://www.bigbinary.com/blog/stdout-sync-true-to-flush-output-immediately"
markdown_url: "https://www.bigbinary.com/blog/stdout-sync-true-to-flush-output-immediately.md"
---

# $stdout.sync = true to flush output immediately

Since IO operations are expensive IO results are not flushed immediately. See
the technique presented here to flush output immediately.

- Author: Neeraj Singh
- Published: July 4, 2009
- Categories: Ruby

Try this.

```ruby
5.times do
  putc('.')
  sleep(2)
end
```

I was hoping that I will get a dot after every two seconds. But that's not what
happens when you run the code. I see nothing for first 10 seconds then I see
five dots in one shot. This is not what I wanted.

I started looking around at the documentation for IO class and found the method
called [sync=](http://www.ruby-doc.org/core/classes/IO.html#M002263) which if
set to true will flush all output immediately to the underlying OS.

The reason why OS does not flush immediately is to minimize the IO operation
which is usually slow. However in this case you are asking OS to not to buffer
and to flush the output immediately.

```ruby
$stdout.sync = true
5.times do
  putc('.')
  sleep(2)
end
```

## Links

- [Human page](https://www.bigbinary.com/blog/stdout-sync-true-to-flush-output-immediately)
