From: Hugh Sasse Staff Elec Eng Date: 2004-04-30T19:11:55+09:00 Subject: Re: Should StringIO support to_str ? On Fri, 30 Apr 2004, daz wrote: > > Hugh Sasse wrote: > > Gavin Sinclair wrote: > > > > > #string > > > > That's better, but I don't see the part with which I initialized. > > > The default open mode is 'w+', so append occurs at file pos = 0. > So, like an existing file, your init. string gets overwritten. > > try StringIO.new("text", 'a+') # or 'a' Oh, I see what is happening now, I think. Thank you. > > (Not defending, just observing ;) No problem. > > > Aside: > StringIO in CVS ( ruby 1.9.0 (2004-04-27) [i586-bccwin32] ) > > #----- > require 'stringio' > > sio = StringIO.new > STDOUT.reopen(sio) > #----- > > C:/TEMP/rb5226.TMP:5:in `reopen': cannot convert StringIO into String (TypeError) Is this because of the lack of to_str or because, to_s doesn't give a string, it gives a StringIO? I ran into this with [x].pack("m*") for some StringIO x. irb(main):009:0> x << 'an example' => # irb(main):010:0> [x].pack('m*') TypeError: cannot convert StringIO into String from (irb):10:in `pack' from (irb):10 irb(main):011:0> So either this is a bug in pack because it doesn't use metronomes -- I mean monotremes, or a bug in StringIO because to_s doesn't do "exactly what it says on the tin". Unless my expectations are wrong. > (I appreciate IO#reopen is undergoing changes; thanks) > Thank you Hugh