How to close files in the ruby ​​pearl "Tables?"

The code below works the same as the first time I run it:

require 'rubygems'
require 'spreadsheet'
book = Spreadsheet.open '/Users/me/myruby/Mywks.xls'
sheet = book.worksheet 0
row = sheet.row(1)
puts row[1]
book.write '/Users/me/myruby/Mywks.xls'

When I run it again, I get more messages like:

/Library/Ruby/Gems/1.8/gems/spreadsheet-0.6.5.9/lib/spreadsheet/excel/reader.rb:1149:in `setup': undefined method `read' for false:FalseClass (NoMethodError)
    from /Library/Ruby/Gems/1.8/gems/spreadsheet-0.6.5.9/lib/spreadsheet/excel/reader.rb:121:in `read'

This suggests that there is a problem with: 1. Closing the Excel spreadsheet or 2. Return to the same spreadsheet that I opened.

  • The ruby ​​gem documentation has no information on closing spreadsheets. The examples end in the book.write statement, as indicated above, if anything. My search here and elsewhere did not result in closing the xls file in the spreadsheet.
  • Spreadsheet documentation allows you to write back to the same file, but suggests it might not be worth it. Is this a problem here? If so, am I writing temporary wks and then renaming it?
+5
2

, :

Spreadsheet.open '/Users/me/myruby/Mywks.xls' do |book|
  sheet = book.worksheet 0
  # book should be closed when the block exits
end
+9

: 1) :

module Spreadsheet
  class Workbook
    attr_accessor :io
  end
end

2)

@book.io.close

0

All Articles