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
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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 = truedefeats 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.