From: Sam Roberts Date: 2007-10-13T04:13:50+09:00 Subject: Re: StringIO is not IO? On Sat, Oct 13, 2007 at 03:37:36AM +0900, Hongli Lai wrote: > Eric Mahurin wrote: > >Since StringIO doesn't use IO to base its implementation, it is not an IO. > > > >Why does it matter? If you use standard duck typing, it shouldn't. > >Class hierarchy doesn't matter with duck typing. Classes are just an > >implementation detail with duck typing. You are best off not looking > >#is_a?, #respond_to?, etc. Just assume the object can do the methods > >you'll be using and use. > > Well, some of my code can write its output to a file (specified by a > filename string) or to an IO object. So my code first checks whether > output.is_a?(IO), which is not the case. > > RMagick seems to do the same thing. I tried making RMagick write its So does RexML. You see same symptoms when you use open-uri, and then try to pass the returned "not quite IO" object to RexML. Its unfortunate the std libs aren't a better example of duck-typing. Search around, I think _why and a few other blogs have talked about how difficult it is to do streaming decoding of XML coming over HTTP with RexML because of this. I think the idiomatic ruby way would be for objects that are "like" an IO object to implement #to_io(), so libs could check for #to_io(). This would be analogous to #to_str(), etc. Sam