ruby 1.9.3 的 iconv 弃用警告 [英] iconv deprecation warning with ruby 1.9.3

查看:102
本文介绍了ruby 1.9.3 的 iconv 弃用警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在运行 rspec 时收到此警告:

<前>/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:240:in `block in require': iconv 将来会被弃用,使用 String#encode 代替.

我在使用 rails 3.1.03.1.13.1.2.rc2 版本时收到相同的警告.似乎它与 sqlite3 gem 相关,但我不确定.ruby 1.9.2 没有警告

有什么建议可以处理吗?

解决方案

您收到此弃用通知是因为某处的库需要 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 => "?")

实际上,这似乎不完美.

对于希望支持 1.8 的 gem 创作者来说,这也导致了一个不太容易的故事:

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 的 gem 才能找到它:

假设你的错误信息是:/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.查看拉取请求,我不是第一个.拉力尚未拉入.

<小时>

根据 gem 的不同,可能有升级版本没有这个错误,所以我建议你先升级你的 gem.如果你不走运,你可能会陷入分叉 gem 以摆脱这个不幸的任务(例如,如果你修复它的拉取请求失败了)

I'm getting this warning when I run rspec:

/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:240:in `block in require': iconv will be deprecated in the future, use String#encode instead.

I get the same warning with rails 3.1.0, 3.1.1, 3.1.2.rc2 versions. Seems it's related to sqlite3 gem, but I'm not sure. There are no warnings with ruby 1.9.2

Any suggestions how to deal with it?

解决方案

You are getting this deprecation notice cause a library somewhere is requiring iconv.

iconv is a gem created by Matz that can be used to convert strings from one format to another.

For example this is often used:

Iconv.iconv('UTF-8//IGNORE', 'UTF-8', content) this little bit of magic takes a UTF-8 string that may have invalid chars and converts it to a proper UTF-8 string.

It has been decided that in Ruby 1.9.3 we should not be using iconv any more and instead use the built-in String#encode. encode is more powerful and allows you more flexibility.

The theory is that the above example could be replaced with:

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

In practice it seems this is imperfect.

This also leads to a less than easy story for gem creators who wish to support 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 => '')


So, you have a gem somewhere that is requiring iconv, to find it:

Assuming your error message is: /gems/activesupport-3.1.0/lib/active_support/dependencies.rb:240

Open up /gems/activesupport-3.1.0/lib/active_support/dependencies.rb on line 240:

Add the line:

p caller if file =~ /iconv/

(just after: load_dependency(file) { result = super })

You will get a big fat stack trace:

 rake --tasks
/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 will be deprecated in the future, use String#encode instead.
["/home/sam/.rvm/gems/ruby-1.9.3-p125/gems/calais-0.0.13/lib/calais.rb:5:in `'", 
.. more omitted ..

This tells me it is the calais gem. Looking through pull requests, I am not the first. The pull has not been yanked in.


Depending on the gem, there may be an upgraded version that does not have this error, so I would recommend you upgrade your gems first. If you are unlucky you may be stuck with the unfortunate task of forking a gem to get rid of this (if for example your pull request to fix it languishes)

这篇关于ruby 1.9.3 的 iconv 弃用警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆