I just came across a diuscussion about C++ smart pointers and Python's
refcount GC.
So I fall in the doubt:
If I call File.open 'my_huge_file' { |fd|
..
}
How should I feel?
I mean: would fd be freed or have I to manually start the GC after the
block?
actually the question could be: when GC gets called ?
The price you pay for not needing to keep track of the ownership and
deallocation of everything you allocate is that you can't be certain when
your object will be deleted. In other words, there's a constructor, but
no destructor.
You can force the GC to run, but this is kind of a hack. (I'm not sure if
you're guaranteed the GC will finish before GC.start returns.) Also,
there are destructors, but they're very hackish and are not encouraged.
In practice, in GC'd languages, objects need to manage their non-memory
resources (like open file descriptors) differently. In C++ the resources
are freed when the object is destroyed. In Ruby, it's typical to make a
function that allocates, yields, and frees.
So to answer your question, File.open works that way. When you pass it a
block, File.open closes the file descriptor after the block is finished.
The fd structure in memory still exists until the GC runs, but as far as
the operating system is concerned, the file is closed.