From: ara.t.howard@... Date: 2006-04-18T03:19:14+09:00 Subject: Re: IO not closed by GC On Tue, 18 Apr 2006, Stephan Maka wrote: > 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. this doesn't fit the posix process model. under posix fd's are managed per process-group, not per process. consider this simple quine: harp:~ > cat a.rb open __FILE__ IO.new(3).close if ARGV.delete '--close' fork ? Process.wait : print(IO.new(3).read) harp:~ > ruby a.rb open __FILE__ IO.new(3).close if ARGV.delete '--close' fork ? Process.wait : print(IO.new(3).read) harp:~ > ruby a.rb --close a.rb:6:in `initialize': Bad file descriptor (Errno::EBADF) from a.rb:6 read this http://www.opengroup.org/onlinepubs/000095399/functions/fork.html in particular bullet 4. gc'd io object would be incompatible with the posix process model. i suppose one could have io.dont_gc but i seems the best POLS approach (not to gc) is the posix route. regards. -a -- be kind whenever possible... it is always possible. - h.h. the 14th dali lama