I presume that what's going on here is a simple matter of defaults. If
you don't pass an initial string to StringIO.new, it constructs one for
itself using the default encoding from the locale, and the encoding
coercion rules mean that the internal string's encoding will always be
the same. StringIO.new has no knowledge of the file encoding at the
location it's called from.
This behaviour seems odd to me. I think a better behaviour would be
*either* to always force a string parameter, so that it never has to
pick a default encoding itself, *or* that it should not make itself an
internal string on #new, but instead #dup the first string it gets
passed as a parameter to #write or #puts and use that instead.
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?
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. If
there were, then yes, that's the way to do it.
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
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.
+1, ri has worked for me once before, but rarely does, and I don't enjoy the
format anyway. I used to build docs and host them with `gem server` but now
I turn off ri and rdoc and just use rdoc.info since it has not only core
docs, but also gems.
Ryan Davis wrote in post #1023415:
> On Sep 23, 2011, at 00:07 , Alex Young wrote:
>
>> $ ri StringIO
>> Nothing known about StringIO
>>
>> is what I get. I never assume ri works.
>
> soooo... instead of fixing it and empowering yourself... you choose...
> what exactly?
rubydoc.info, usually. Saves fixing it on every single box I ever
touch.