July 4, 2009
Try this.
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= 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.
$stdout.sync = true
5.times do
putc('.')
sleep(2)
end
If this blog was helpful, check out our full blog archive.