From: Eric Hodel Date: 2011-09-21T03:30:38+09:00 Subject: Re: StringIO and encodings On Sep 20, 2011, at 8:32 AM, Alex Young wrote: > Adam Prescott wrote in post #1022947: >> On Tue, Sep 20, 2011 at 4:18 PM, Alex Young >> wrote: >>> StringIO.new has no knowledge of the file encoding at the >>> location it's called from. >> >> Can it not be changed so that it knows the internal encoding, instead? >> That would stop you having to break the argument-less constructor or doing >> any #dup'ing, no? > > I don't know if there's an API for that, but I suspect there isn't. It's not that hard to check: $ ri StringIO | grep encoding external_encoding internal_encoding set_encoding > If there were, then yes, that's the way to do it. $ ri StringIO.set_encoding StringIO.set_encoding (from ruby core) ------------------------------------------------------------------------------ strio.set_encoding(ext_enc, [int_enc[, opt]]) => strio ------------------------------------------------------------------------------ Specify the encoding of the StringIO as ext_enc. Use the default external encoding if ext_enc is nil. 2nd argument int_enc and optional hash opt argument are ignored; they are for API compatibility to IO.