Ruby-on-rails – iconv deprecation warning with ruby 1.9.3

rspecrubyruby-on-rails

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?

Best Answer

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)

Related Topic