Friday, June 29, 2012

Ruby error "invalid multibyte character (US-ASCII)"

If you try to include non-ASCII characters in the source code of a Ruby program, you may get the error "invalid multibyte character (US-ASCII)". This one-line program illustrates the problem, using the right angle quote character: foo = "»" If you Google the error message, you'll find a lot of advice about placing "# encoding: UTF-8" or "# coding UTF-8" or "$KCODE = 'u'" at the top of the file. None of these worked for me. However, I found a simple fix. My environment is Ruby 1.9.3p194 on Windows. I opened my .rb file in Notepad, selected Save As, chose "UTF-8" from the "Encoding" dropdown, and clicked "Save". This -- with no other changes made -- solved the problem.

1 comment:


  1. Very informative error.Thank you author for posting this kind of errors .

    http://www.wikitechy.com/fix-error/ruby-invalid-multi-byte-character



    Both are really good.
    Cheers,
    Venkat

    ReplyDelete