views:

490

answers:

1

I had an issue where downloading to a local file was locking it until I killed the ruby script. On a hunch, I changed the form of the call and now it doesn't lock the file.

Can someone tell me why the second form below doesn't lock the file but the first one does?

This form locks the local file:

open(ver_local_zip, "w+").write(open(remote_zip, :proxy=>ftp_proxy).read)

This form doesn't lock the file:

open(ver_local_zip, "w+") { |f| f.write(open(remote_zip, :proxy=>ftp_proxy).read) }

Thanks.

+2  A: 

Calling the method open with a block usually closes the file immediately after the execution of the block finished. Without a block, open return a file (handle) that will be freed when the next garbage collection takes place.

jug
What I suspected. Thanks for answering.
digitalsanctum
Note that you can (and should) free the returned handle manually when using the first method rather than wait for the GC.
Eli