From: Brian Candler Date: 2011-09-11T01:52:35+09:00 Subject: Re: how to write data in binary to a file? 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") => # irb(main):002:0> f2 = File.open("/tmp/f2","wb") => # irb(main):003:0> f1.external_encoding => nil irb(main):004:0> f2.external_encoding => # 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]") -- Posted via http://www.ruby-forum.com/.