From: Brian Candler Date: 2012-09-20T16:58:52+09:00 Subject: Re: Encoding question Nathan Beyer wrote in post #1076380: > Once you do know the encoding, the proper > way to open up a file is to declare the external encoding of the file, > like this - > > f = File.open('somefile.txt', 'r:iso-8859-1') That is correct. > Then when you read content from the file, the data in the file will be > transcoded from 'iso-8859-1' to the default internal encoding of the > Ruby interpreter instance (generally UTF-8). That is incorrect. But encodings in ruby are such a damned mess that I'm not surprised that many people don't understand it. *No* transcoding takes place in the above example, regardless of the where it runs; the data is read into the string as-is. However, every String which you read from the file using f.gets or f.getc is marked to say that it is encoded using UTF-8. (But not Strings which you read using f.read) > You can define what > internal encoding to use by further qualifying the open, like this - > > f = File.open('somefile.txt', 'r:iso-8859-1:utf-8') Yes, in *that* case, ruby will transcode. > Check out this article for some more information - > http://nuclearsquid.com/writings/ruby-1-9-encodings/. For as much of the gorey details as I managed to work out before giving up, you can also try https://github.com/candlerb/string19/blob/master/string19.rb -- Posted via http://www.ruby-forum.com/.