Ruby 1.9: gem installで文字コード変換エラー(unable to convert "\xXX" to UTF-8 in conversion from ASCII-8BIT to UTF- 8 to Windows-31J)
Ruby 1.9.3をRubyInstaller for Windowsでインストール後、railsやnokogiriを入れようとしたら表題のエラーがいくつかでた。
> gem install nokogiri Fetching: nokogiri-1.5.6-x86-mingw32.gem (100%) Successfully installed nokogiri-1.5.6-x86-mingw32 1 gem installed Installing ri documentation for nokogiri-1.5.6-x86-mingw32... unable to convert "\xE3" to UTF-8 in conversion from ASCII-8BIT to UTF- 8 to Windows-31J for CHANGELOG.ja.rdoc, skipping unable to convert "\xE8" to UTF-8 in conversion from ASCII-8BIT to UTF- 8 to Windows-31J for CHANGELOG.rdoc, skipping unable to convert "\xE9" to UTF-8 in conversion from ASCII-8BIT to UTF- 8 to Windows-31J for README.ja.rdoc, skipping unable to convert "\xE2" to UTF-8 in conversion from ASCII-8BIT to UTF- 8 to Windows-31J for ext/nokogiri/xml_node_set.c, skipping Installing RDoc documentation for nokogiri-1.5.6-x86-mingw32... unable to convert "\xE3" to UTF-8 in conversion from ASCII-8BIT to UTF- 8 to Windows-31J for CHANGELOG.ja.rdoc, skipping unable to convert "\xE8" to UTF-8 in conversion from ASCII-8BIT to UTF- 8 to Windows-31J for CHANGELOG.rdoc, skipping unable to convert "\xE9" to UTF-8 in conversion from ASCII-8BIT to UTF- 8 to Windows-31J for README.ja.rdoc, skipping unable to convert "\xE2" to UTF-8 in conversion from ASCII-8BIT to UTF- 8 to Windows-31J for ext/nokogiri/xml_node_set.c, skipping >
railsでは下記のエラーも出ていた。
unable to convert U+00E9 from UTF-8 to Windows-31J for lib/active_support/multibyte/chars.rb, skipping unable to convert U+00C0 from UTF-8 to Windows-31J for lib/active_support/vendor/i18n-0.4.1/i18n/backend/transliterator.rb, skipping unable to convert U+00A0 from UTF-8 to Windows-31J for lib/active_support/vendor/i18n-0.4.1/i18n.rb, skipping
エラーが出ているファイルはドキュメントなので動作に問題はないはずだが、気になったのでgemを削除して解決法を調べた。
要するに、UTF-8からShift_JIS(Windows-31J)への変換ができなくてエラーになっている。
コマンドプロンプトの文字コードをchcpコマンドでUTF-8に変えたらエラーはなくなった。
> chcp 65001 Active code page: 65001 > gem install nokogiri Fetching: nokogiri-1.5.6-x86-mingw32.gem (100%) Successfully installed nokogiri-1.5.6-x86-mingw32 1 gem installed Installing ri documentation for nokogiri-1.5.6-x86-mingw32... Installing RDoc documentation for nokogiri-1.5.6-x86-mingw32... >
文字コードを元にもどすには chcp 932 と入力すればよい。
Cygwinなら環境変数LANGをUTF-8にすれば済むかも。
ちなみに、中国語系のサイトにDevKitをいれたらエラーが消えたような記述があったが、自分の環境では解決できなかった。