From: Peter Zotov Date: 2011-12-10T00:04:16+09:00 Subject: Re: Close? ... or have I been messing up from the get go? Wayne Brisette писал 09.12.2011 18:42: > While doing some reading this morning on nokogiri, I noticed that > they used this opening a file: > > f = File.open("blossom.xml") > doc = Nokogiri::XML(f) > f.close > > when I've read files, I've never closed them! I've always been under > the assumption that reading a file closed the I/O stream > automatically > when completed. Have I been under the wrong assumption from the get > go? > > Wayne No, files aren't automatically closed upon encountering EOF. You may want, for example, rewind the file cursor to the start or middle of the file. On the other hand, this construct automatically closes file after execution leaves the block: doc = File.open("blossom.xml") do |f| Nokogiri::XML(f) end Note that File.open returns the value which the block has returned, allowing you to write such constructs. Most of the other yielding methods do it a bit different: for example, Array (and, generally, anything Enumerable) returns self to allow for method chaining. -- WBR, Peter Zotov.