Ruby 2.0 iconv 替换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16032241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ruby 2.0 iconv replacement
提问by cnd
I don't know Ruby but want to run an script where:
我不知道 Ruby 但想运行一个脚本,其中:
D:/Heather/Ruby/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require': cannot load such file -- iconv (LoadError)
D:/Heather/Ruby/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require': 无法加载此类文件 -- iconv (LoadError)
it works somehow if I comment iconv code but it will be much better if I can recode this part:
如果我评论 iconv 代码,它会以某种方式工作,但如果我可以重新编码这部分代码会好得多:
return Iconv.iconv('UTF-8//IGNORE', 'UTF-8', (s + ' ') ).first[0..-2]
without iconv. Maybe I can use String#encodehere somehow?
没有iconv. 也许我可以String#encode在这里使用?
回答by Dane Balia
Iconv was deprecated (removed) in 1.9.3. You can still install it.
Iconv 在 1.9.3 中被弃用(删除)。您仍然可以安装它。
Reference Material if you unsure: https://rvm.io/packages/iconv/
如果您不确定,请参考:https: //rvm.io/packages/iconv/
However the suggestion is that you don't and rather use:
但是,建议您不要使用,而是使用:
string.encode("UTF-8", :invalid => :replace, :undef => :replace, :replace => "?")
回答by masakielastic
String#scrubcan be used since Ruby 2.1.
String#scrub从 Ruby 2.1 开始就可以使用了。
str.scrub(''),
str.scrub{ |bytes| '' }
Related question: Equivalent of Iconv.conv(“UTF-8//IGNORE”,…) in Ruby 1.9.X?
回答by David Waller
If you're not on Ruby 2.1, so can't use String#scrubthen the following will ignore all parts of the string that aren't correctly UTF-8 encoded.
如果您使用的不是 Ruby 2.1,因此无法使用,String#scrub那么以下内容将忽略未正确 UTF-8 编码的字符串的所有部分。
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!)
encode 方法几乎完全符合您的要求,但需要注意的是,如果 encode 认为字符串已经是 UTF-8 则不会执行任何操作。因此,您需要更改编码,通过一种仍然可以编码 UTF-8 可以编码的完整 unicode 字符集的编码。(如果你不这样做,你会破坏任何不在该编码中的字符——7 位 ASCII 将是一个非常糟糕的选择!)
回答by jrochkind
I have not had luck with the various approaches using a one line string.encode by itself
我对使用一行 string.encode 本身的各种方法没有运气
But I wrote a backfill that implements String#scrub in MRI pre 2.1, or other rubies that do not have it.
但是我写了一个回填,它在 MRI pre 2.1 或其他没有它的 rubies 中实现了 String#scrub。

