From: Gary Wright Date: 2011-09-13T03:54:13+09:00 Subject: Re: how to write data in binary to a file? On Sep 12, 2011, at 7:20 AM, Robert Klemme wrote: > >> Now, if you open a file for write and set an external encoding (the >> default is nil) it means "transcode to this encoding". But for some >> reason, setting external encoding to ASCII-8BIT bypasses this rule. > > See above. The name ASCII-8BIT is probably not the best one to choose > for this but if you think about it as BINARY then everything fits > nicely together. I think the anomaly that Brian pointed out was that the transcoding during IO behaved differently than explicit transcoding: > Normally, transcoding a UTF-8 string (which contains non-ASCII > characters) to ASCII-8BIT would raise an exception: > > irb(main):006:0> "groß".encode("ASCII-8BIT") > Encoding::UndefinedConversionError: U+00DF from UTF-8 to ASCII-8BIT > > But now we have a special case where it *doesn't* raise an exception :-( It makes sense to me that transcoding to 'binary' (or 'ASCII-8BIT') would just copy bytes but it doesn't make sense that implicit transcoding via IO and explicit transcoding via #encode behave differently. Gary Wright