Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "file.sync = true" do?

Tags:

file

ruby

I had a look at the docs but I can't seem to find the relevant part. Can anyone tell me what the call to sync is for in the following code?

fh = Tempfile.new('tmp')
fh.sync = true 
like image 807
David Tuite Avatar asked Jan 20 '12 22:01

David Tuite


1 Answers

It sets the sync mode of the file.

This affects future operations and causes output to be written without block buffering.

If f.tty? is true, that is, if the file is connected to a console-like device, then output is not block buffered. But when output goes to a pipe or file, f.tty? will be false and the I/O library will switch to block buffering, that is, accumulating output in a buffer and writing it only if the file is closed, the program exits, or the buffer fills up. This is faster and the end result is the same.

Setting f.sync = true defeats this switch. This can be useful if the output of the pipe is connected to something that actually is a console or in some way interactive or if the contents of the file are being actively monitored.

like image 152
DigitalRoss Avatar answered Oct 07 '22 01:10

DigitalRoss