From: "David A. Black" Date: 2008-03-15T22:31:01+09:00 Subject: Re: baffling undefined method error Hi -- On Sat, 15 Mar 2008, Tom Cloyd wrote: > I'm getting an error I can't account for. In a method I've written I open a > file and access a yaml store: > > archive_stored_list = 'archive_stored_list.yml' > archives_stored_catalog = File.open(archive_stored_list) {|i| YAML.load(i)} > > I massage the data a bit, and want to write it back out to the same file. I > try to close the file so I can open it for output, but get an error: > > archives_stored_catalog.close # <= "undefined method `close'" error is > written to log file here > open(archive_stored_list,"w") {|i| YAML.dump(archives_stored_catalog,i)} > > Why is this giving an error? Isn't the IO.close method always available? Very > confusing. > > This usage is straight out of Programming Ruby 2nd edition, but it doesn't > work for me, and I can't see the error. When you use a block with File.open, the filehandle is closed automatically when the block exits. There's no point even saving it, because the value of the whole operation will be nil. David -- Upcoming Rails training from David A. Black and Ruby Power and Light: ADVANCING WITH RAILS, April 14-17 2008, New York City CORE RAILS, June 24-27 2008, London (Skills Matter) See http://www.rubypal.com for details. Berlin dates coming soon!