From: Gennady Bystritsky Date: 2006-04-18T03:33:38+09:00 Subject: Re: IO not closed by GC Even GC.start does not guarantee that the garbage collection will be actually started, it is just a hint after all. And I am not sure that even when an IO object is being garbage collected, there will be an implicit call to close(). I personally would not expect it as no finalizers are installed by default. So the programmer must take care to write the correct code, short or long ;-). Even if GC.start helped in your case, the overall code would be longer (because of calls to GC.start) and work much worse (as GC does a lot that you may not need for the moment) than what I have suggested. And using blocks with popen is GUARANTEED to close descriptors properly. See my point? By the way, you are welcome. Gennady. > -----Original Message----- > From: Stephan Maka [mailto:stephan@spaceboyz.net] > Sent: Monday, April 17, 2006 10:37 > To: ruby-talk ML > Subject: Re: IO not closed by GC > > Gennady Bystritsky wrote: > > But you do not even know when GC will be run, so in your > case it might > > Inserting 'GC.start; sleep 1' won't help... > > > not even have kicked in. On the other hand, IO.popen has a > nice block > > form that gurantees that an underlying IO object will be closed > > properly. You can rewrite your code as: > > > > os = IO.popen('uname -sr') { |io| io.readlines.to_s.strip } > > That works, but is longer. Shorter code is more readable in general... > > Anyway, I think an IO object should be closed when not being > needed anylonger. > > > Stephan. >