From: Robert Klemme Date: 2006-04-19T02:02:06+09:00 Subject: Re: IO not closed by GC 2006/4/17, Stephan Maka : > 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... AFAIK GC is not guaranteed to start when GC.start is invoked plus it might not even be guaranteed that this particular object is collected at all. It seems to be a common misconception about GC that instances that are not referenced any more are immediately cleaned up and do not use resources. > > 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... I don't find the block form less readable than your piece of code. > Anyway, I think an IO object should be closed when not being needed > anylonger. The difficult part here is to decide when an object is "not needed any longer". Since Ruby has no destructors and automatic variables the block form is the most appropriate way to deal with cleanup. I suggest you just get used to it. Kind regards robert -- Have a look: http://www.flickr.com/photos/fussel-foto/