7stud -- wrote in post #1021082:
>> I want to write the data into a file in binary mode. The file is opened
>> in binary mode by ofile=File.open("filename",'wb').
>
> All that does is turn off newline conversions, so if you write \n to a
> file, \n will be written to the file no matter what operating system the
> program is running on.  Well, and this:
>
> ===
> ...
> And sets external encoding to ASCII-8BIT unless explicitly specified.
> ===

Oh no... yet another inconsistency from ruby 1.9.

The documentation you quote is correct in what it says:

irb(main):001:0> f1 = File.open("/tmp/f1","w")
=> #<File:/tmp/f1>
irb(main):002:0> f2 = File.open("/tmp/f2","wb")
=> #<File:/tmp/f2>
irb(main):003:0> f1.external_encoding
=> nil
irb(main):004:0> f2.external_encoding
=> #<Encoding:ASCII-8BIT>

However, the behaviour this implies is unexpected:

irb(main):005:0> f2.write("gro")
=> 5

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 :-(

Regards,

Brian.

(Tested with "ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-linux]")