From: Brian Candler Date: 2011-08-12T17:22:56+09:00 Subject: Re: Problems with Accent in Ruby 1.9+ (Latin Characters) Regis d'Aubarede wrote in post #1016237: > I think that Phillip Gawlowski is right. > I have try to restated this kind of issue, I get only > "invalid multibyte char" exception : > >> invalid multibyte char (US-ASCII) (SyntaxError) >> encoding.rb:2: invalid multibyte char (US-ASCII) That's a *syntax error*, i.e. your program itself can't be parsed. In this case, I think you have an accented character in a string literal, but you have forgotten to put #encoding: UTF-8 at the top of your file (did you remove it?). Without such a line, the source encoding defaults to US-ASCII, which means that any character with the top bit set is *forbidden* entirely. One of the problems with ruby 1.9 and encodings is that nobody understands it properly, and as a result lots of people give incorrect advice. Of course, you *can* just try making random changes to your program in the hope that it will help, and sometimes you will stumble on something which works. But, let's assume you don't want to do it by guesswork. So: (1) Put #encoding: UTF-8 as the first line of your file (or the second, if you have a Unix shebang line). Then all your string literals will be tagged UTF-8 (2) Using :encoding=>"UTF-8" when you open or read any file. Then all your read-in strings will be tagged UTF-8 (3) Then you can concatenate these strings, or interpolate one into the other, and it will work. ruby 1.9 may still crash, but in fewer circumstances (e.g. if you try a regexp match against your string and the string contains invalid UTF-8 characters. There are some operations you can do on invalid strings successfully, and some which crash. See the link on github I gave before) > On windows, I do all my stuff with 1.9.2 in utf-8, and i forget 1.8... > Issue arrive when I test compatibility with JRuby ! JRuby has both 1.8 and 1.9 compatibility modes, or at least it did the last time I looked at it a while back. -- Posted via http://www.ruby-forum.com/.