Bug #3533


Default external encoding not picked up by output unless internal encoding set

Added by pdwilson12 (Peter Wilson) almost 12 years ago. Updated about 11 years ago.

Target version:
ruby -v:
ruby 1.9.2dev (2010-07-02 revision 28522) [x86_64-darwin10.4.0]


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 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.


EncodingTest.rb (579 Bytes) EncodingTest.rb pdwilson12 (Peter Wilson), 07/04/2010 11:01 AM
Actions #1

Updated by runpaint (Run Paint Run Run) almost 12 years ago

I believe both behaviours are intended.

As I note in , "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.

Actions #2

Updated by naruse (Yui NARUSE) almost 12 years ago

  • Status changed from Open to Rejected

Run Paint is right; it is intended.

Actions #3

Updated by pdwilson12 (Peter Wilson) almost 12 years ago

It is very odd that writeable IO objects (but not read-only IO objects) ignore the default external encoding unless a default internal encoding is set. But Ok.


Also available in: Atom PDF