File.new("file.ext", "w").write data

E

Erik Veenstra

Consider...

File.new("file.ext", "w").write data

Is this safe? When will the file be closed? For IO it's said:
"I/O streams are automatically closed when they are claimed by
the garbage collector." Is this true for files as well, (File
is a subclass of IO)?

I know you can do...

File.open("file.ext", "w"){|f| f.write data}

...., but the first one is shorter and better readable.

gegroet,
Erik V.
 
L

Lennon Day-Reynolds

Erik,

The file handle will indeed stay open until the File instance is GC'd.
The block-passing version is a few extra characters, but is much
better for long-running processes, or those which open large numbers
of files, as it automatically closes the file at the end of the block.

Lennon
 
A

Alexey Verkhovsky

File.new("file.ext", "w").write data

Garbage collector will undoubtedly close it, but it is a very unhealthy
habit to rely on garbage collector to close your OS resources for you.

Garbage objects can remain uncollected for hours.

In brief, it is one of those things that you can get away with for a
while, and then you get an expensive lesson in defensive coding.

Best regards,
Alexey Verkhovsky
 
C

Caio Chassot

Erik said:
Consider...

File.new("file.ext", "w").write data
...
I know you can do...

File.open("file.ext", "w"){|f| f.write data}

...., but the first one is shorter and better readable.


If being short and readable is your concern, you can always extend the
class to do what you want. something like:

class << File
def write_at_once fn, data
self.open(fn,'w'){|f|f.write(data)}
end
end


File.write_at_once('file.ext', data)
 
J

Joel VanderWerf

Erik said:
Consider...

File.new("file.ext", "w").write data

I don't think it can be collected during the #write method, because the
File object is the value of "self" during the method. It's reachable, so
it shouldn't be collected.

Still, there are good reasons to use the block form, as others have
pointed out.
 
R

Robert Klemme

Erik Veenstra said:
Consider...

File.new("file.ext", "w").write data

Is this safe? When will the file be closed? For IO it's said:
"I/O streams are automatically closed when they are claimed by
the garbage collector." Is this true for files as well, (File
is a subclass of IO)?

Yes. But you won't know when it is closed. That can be a nuisance if you
have to open the same file multiple times - it may work or not, depending
on the timing of GC. But that's usually a bad choice.
I know you can do...

File.open("file.ext", "w"){|f| f.write data}

..., but the first one is shorter and better readable.

.... and less safe. Use the block form, that's IMHO the most appropriate
thing to do here. It's not much longer also.

Regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,017
Latest member
GreenAcreCBDGummiesReview

Latest Threads

Top