What STDOUT.sync = true means?

13,949

Normally puts does not write immediately to STDOUT, but buffers the strings internally and writes the output in bigger chunks. This is done because IO operations are slow and usually it makes more sense to avoid writing every single character immediately to the console.

This behavior leads to problems in certain situations. Imagine you want to build a progress bar (run a loop that outputs single dots between extensive calculations). With buffering the result might be that there isn't any output for a while and then suddenly multiple dots are printed at once.

To avoid this behavior and instead write immediately to STDOUT you can set STDOUT into sync mode like this:

STDOUT.sync = true

From the docs:

When sync mode is true, all output is immediately flushed to the underlying operating system and is not buffered internally.

Share:
13,949
SuperManEver
Author by

SuperManEver

Updated on June 15, 2022

Comments

  • SuperManEver
    SuperManEver almost 2 years

    I am reading source code for god A process monitoring framework in Ruby and found this STDOUT.sync = true. I've never seen something like this before.

    Please explain what it does, what the point of this line?

    Thanks in advance.