我在运行 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.
我在使用 Rails 3.1.0
、3.1.1
、3.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 => "?")
实际上这似乎是 imperfect .
对于希望支持 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
打开 /gems/activesupport-3.1.0/lib/active_support/dependencies.rb
第 240 行:
添加行:
p caller if file =~ /iconv/
(就在:load_dependency(file) { result = super }
之后)
你会得到一个大的堆栈跟踪:
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 ..
这告诉我它是 calais gem 。通过拉取请求,我 am not the first .拉力还没有被拉进去。
根据 gem 的不同,可能有升级版本没有这个错误,所以我建议您先升级您的 gem。如果你不走运,你可能会遇到一个不幸的任务,那就是 fork 一个 gem 来摆脱这个(例如,如果你修复它的拉取请求失败了)
关于ruby-on-rails - ruby 1.9.3 的 iconv 弃用警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8148762/