From: Phillip Gawlowski Date: 2007-03-25T00:27:19+09:00 Subject: Re: scope and RAII jeff_alexander_44@yahoo.com wrote: > Does ruby make a special effort to release a variable when it goes out > of scope? The canonical example is > > File.open("foo.txt") { |f| > print f.read > } > # internal file handle released here > > Is this behavior a general rule, or just a special case for > File.open()? And is it released immediately or upon the > next GC pass? The file handle should be released ASAP if you use: file = File.open("foo", "r") #opens foo in read-only mode file.readlines { |f| print f } #prints each line file.close #closes the file handle. In that case, you can use the mixins from IO via File, including, but not limited, to IO#close. IMHO, it is a good idea to tell Ruby the mode it should open a file, too.[0] Disclaimer: I'm still a Ruby nuby, and don't know anything about the inner workings of Ruby itself. Bibliography: [0] http://www.zenspider.com/Languages/Ruby/QuickRef.html#15 -- Phillip "CynicalRyan" Gawlowski Rule of Open-Source Programming #20: Open Code != Good Code