[英]Ruby 2.0 iconv replacement
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:/Hather /Ruby/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:在`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
. 没有
iconv
。 Maybe I can use String#encode
here somehow? 也许我可以在某种程度上使用
String#encode
#coding?
Iconv was deprecated (removed) in 1.9.3. Iconv在1.9.3中被弃用(删除)。 You can still install it.
你仍然可以安装它。
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 => "?")
String#scrub can be used since Ruby 2.1. 从Ruby 2.1开始,可以使用String#scrub 。
str.scrub(''),
str.scrub{ |bytes| '' }
Related question: Equivalent of Iconv.conv(“UTF-8//IGNORE”,…) in Ruby 1.9.X? 相关问题: Ruby 1.9.X中的Iconv.conv(“UTF-8 // IGNORE”,...)的等价物?
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. 如果你不在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. 编码方法几乎完全符合你的要求,但是如果认为字符串已经是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.
因此,您需要更改编码,通过仍然可以编码UTF-8可以编码的全套unicode字符的编码。 (If you don't you'll corrupt any characters that aren't in that encoding - 7bit ASCII would be a really bad choice!)
(如果你不这样做,你会破坏那些不在那种编码中的字符 - 7bit ASCII将是一个非常糟糕的选择!)
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中实现String#scrub的回填,或其他没有它的红宝石。
https://github.com/jrochkind/scrub_rb https://github.com/jrochkind/scrub_rb
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.