25

Ruby はわかりませんが、次の場所でスクリプトを実行したいと考えています。

D:/Heather/Ruby/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:`require' で: そのようなファイルを読み込めません -- iconv (LoadError)

iconv コードにコメントを付けると何とか機能しますが、この部分を再コーディングできれば、はるかに良くなります。

return Iconv.iconv('UTF-8//IGNORE', 'UTF-8', (s + ' ') ).first[0..-2]

なしiconvString#encodeここは何とか使えないかな。

4

4 に答える 4

39

Iconv は 1.9.3 で廃止 (削除) されました。まだインストールできます。

不明な場合の参考資料: https://rvm.io/packages/iconv/

ただし、提案は、使用しないで、むしろ使用することです。

string.encode("UTF-8", :invalid => :replace, :undef => :replace, :replace => "?")

API

于 2013-04-16T08:39:05.253 に答える
9

Ruby 2.1 からString#scrubが使えるようになりました。

str.scrub(''),
str.scrub{ |bytes| '' }

関連する質問: Ruby 1.9.X の Iconv.conv(“UTF-8//IGNORE”,…) と同等ですか?

于 2013-06-08T09:24:06.560 に答える
8

If you're not on Ruby 2.1, so can't use String#scrub then the following will ignore all parts of the string that aren't correctly UTF-8 encoded.

string.encode('UTF-16', :invalid => :replace, :replace => '').encode('UTF-8')

The encode method does almost exactly what you want, but with the caveat that encode doesn't do anything if it thinks the string is already UTF-8. So you need to change encodings, going via an encoding that can still encode the full set of unicode characters that UTF-8 can encode. (If you don't you'll corrupt any characters that aren't in that encoding - 7bit ASCII would be a really bad choice!)

于 2014-01-23T17:43:43.873 に答える