From: "Haase, Konstantin" Date: 2011-10-26T03:56:02+09:00 Subject: [ruby-core:40368] Re: [ruby-trunk - Feature #5479] import StringIO into core, add String#to_io Maybe another idea would be to introduce a Streamable protocol/class/module, and add a to_stream method both to IO and StringIO. It could also ease implementing your own IO-like behavior. Konstantin On Oct 25, 2011, at 11:35 , Aaron Patterson wrote: > On Wed, Oct 26, 2011 at 02:22:21AM +0900, Haase, Konstantin wrote: >> My main request was to add String#to_io, as Aaron described, so this protocol can actually be used. This is the only reason why I proposed moving StringIO to core. We could also add String#to_io as a monkey-patch to String in stringio in the stdlib. > > I really like this idea, but I'm kind of afraid of the consequences. > StringIO isn't a subclass of IO, and some things in ruby call `to_io` > then check the return type: > > irb(main):001:0> require 'stringio' > => true > irb(main):002:0> File.exist? 'hello!' > => false > irb(main):003:0> class String; def to_io; StringIO.new self; end end > => nil > irb(main):004:0> File.exist? 'hello!' > TypeError: can't convert String to IO (String#to_io gives StringIO) > from (irb):4:in `exist?' > from (irb):4 > from /Users/aaron/.local/bin/irb:12:in `
' > irb(main):005:0> > > I'm not sure which is best: convert StringIO to subclass IO, or find > these places that call `to_io` and fix them. Not to mention, how do we > "fix" the above example? What does `exists?` mean in terms of a > StringIO? > > -- > Aaron Patterson > http://tenderlovemaking.com/