This post originated from an RSS feed registered with Ruby Buzz
by Daniel Berger.
Original Post: Spreadsheet II: The Spreadsheet Strikes Back
Feed Title: Testing 1,2,3...
Feed URL: http://djberg96.livejournal.com/data/rss
Feed Description: A blog on Ruby and other stuff.
I've had 3 releases of the spreadsheet package in as many days. The first one didn't get along with gems. The second one didn't get along with Windows. Now I'm discovering that the third one doesn't get along with StringIO.
It's not my fault! The check is in the mail! She was dead when I got there! Etc, etc.
The problem stems from the fact that, in proper duck typing fashion, Excel.new checks to see if the argument responds to a fileno method. There is a StringIO#fileno, but it always returns nil. Kerblooey.
I say it's not my fault, because there's no way to automatically delegate IO methods to a StringIO object as far as I can tell. Consider:
That won't work. I don't see any way to make that work without special handling in, say, a subclass. However, since I *am* dealing with a subclass (OLEWriter), I *could* make it work. The only solution I see is to do a type check (ick) in the constructor, specifically looking for instances of the StringIO class. Then, with Forwardable, automatically forward all IO methods to the StringIO instance. Something like this:
require "stringio"
require "forwardable"
class MyIO < IO
include Forwardable
def initialize(arg)
if arg.kind_of?(StringIO)
@io = arg
delegate IO.instance_methods(false) => :@io
else
...
end
end
end
That may be what I'll do for 0.3.3. However, I'll use my cleaned up version of Forwardable (which Matz has tentatively accepted) rather than the current version in the stdlib, which spews ugly warnings.