From: Brian Candler Date: 2010-05-11T18:10:28+09:00 Subject: Re: puts bug? Roger Pack wrote: > With this file: > > # coding: UTF-8 > puts "asdfМикимаус" > > output in windows is: > > C:\dev\digitalarchive_trunk>ruby -v go.rb > ruby 1.9.3dev (2010-05-11 trunk 27724) [i386-mingw32] > asdf╨£╨╕╨║╨╕╨╝╨░╤â╤ü > > > Is this expected? Yes. One of the (many arbitrary) rules of ruby 1.9 encodings is that although there is an external_encoding set on STDIN, there is no external encoding set on STDOUT. So when you write a string to STDOUT, by default the sequence of bytes is transferred as-is to the terminal. If you want strings to be transcoded on write, try putting this at the start of your program: STDOUT.set_encoding Encoding.locale_charmap or STDOUT.set_encoding Encoding.default_external or STDOUT.set_encoding "IBM437" > Also shouldn't the following code fail? > > "asdfМикимаус".force_encoding "IBM437" > > (it doesn't) No, it shouldn't; you're just tagging the string with a different encoding. In any case, notice that str = "asdfМикимаус".force_encoding "IBM437" str.valid_encoding? returns true. This means that the sequence of bytes in str happens to be a valid sequence of characters in IBM437 (just not the characters you originally thought of) However, the following does give an error, because you're trying to transcode each character in the source string to an equivalent character in IBM437: "asdfМикимаус".encode "IBM437" # Encoding::UndefinedConversionError: U+041C from UTF-8 to IBM437 If you want to understand how String encodings work in ruby 1.9 - and I warn you in advance that you probably don't - you could try reading http://github.com/candlerb/string19/blob/master/string19.rb Then cry. Regards, Brian. -- Posted via http://www.ruby-forum.com/.