Default external encoding not picked up by output unless internal encoding set
ruby 1.9.2dev (2010-07-02 revision 28522) [x86_64-darwin10.4.0]
If I specify an external encoding with the -E option, the STDOUT and File.open output streams do not respect it. I have to explicitly assign an external encoding on the stream, otherwise strings written to the stream are not transcoded; instead the raw byes of the strings are written out. However, if I set a default internal encoding with the -E option, it all works. Strings with some arbitrary encoding are transcoded into the default external encoding when written to the stream.
I've attached a sample program illustrating this. It logs ruby's default encodings, the encodings of STDOUT and STDIN, and then writes an ISO-8859-1 and UTF-8 string to a new file opened without an explicit encoding. If I run the script with '-E utf-8', STDOUT and the file don't list any external encoding and the generated file has text of both encodings present. If I instead run it with '-E utf-8:utf-8', adding an internal encoding, all streams get the UTF-8 external encoding and the generated file contains the correct UTF-8 versions of both strings.
Updated by runpaint (Run Paint Run Run) about 10 years ago
I believe both behaviours are intended.
As I note in http://ruby.runpaint.org/encoding#external , "The external encoding of a stream may be queried with IO#external_encoding, which returns the corresponding Encoding object. Note, however, that if the stream is in write-only mode, and wasn’t explicitly assigned an external encoding, this method returns nil."
When the internal encoding is nil, no transcoding occurs, and it is nil by default. If you want transcoding you must ask for it explicitly.