您收到此弃用通知是因为某处的某个库需要 iconv。
iconv 是由Matz 创建的 gem,可用于将字符串从一种格式转换为另一种格式。
例如经常使用这个:
Iconv.iconv('UTF-8//IGNORE', 'UTF-8', content) 这个小魔法将一个可能包含无效字符的 UTF-8 字符串转换为正确的 UTF-8 字符串。
已经决定在 Ruby 1.9.3 中我们不应该再使用 iconv 而是使用内置的String#encode。 encode 更强大,让您更灵活。
理论上,上面的例子可以替换为:
string.encode("UTF-8", :invalid => :replace, :undef => :replace, :replace => "?")
实际上,这似乎是imperfect。
对于希望支持 1.8 的宝石创建者来说,这也导致了一个不太容易的故事:
content = RUBY_VERSION.to_f < 1.9 ?
Iconv.iconv('UTF-8//IGNORE', 'UTF-8', "content") :
"#{content}".encode(Encoding::UTF_8, :invalid => :replace, :undef => :replace, :replace => '')
所以,你有一个宝石需要 iconv 才能找到它:
假设你的错误信息是:/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:240
在第 240 行打开/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:
添加行:
p caller if file =~ /iconv/
(紧随其后:load_dependency(file) { result = super })
你会得到一个很大的堆栈跟踪:
耙--任务
/home/sam/.rvm/gems/ruby-1.9.3-p125/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:251:in `block in require': iconv 将在未来被弃用,请改用 String#encode。
["/home/sam/.rvm/gems/ruby-1.9.3-p125/gems/calais-0.0.13/lib/calais.rb:5:in `'",
..更多省略..
这告诉我它是calais gem。查看拉取请求,我am not the first。拉力尚未拉入。
根据gem的不同,可能有升级版本没有这个错误,所以我建议你先升级你的gem。如果你不走运,你可能会被困在 fork 一个 gem 以摆脱这个不幸的任务(例如,如果你修复它的 pull request 失败了)