25

I don't know Ruby but want to run an script where:

D:/Heather/Ruby/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require': cannot load such file -- iconv (LoadError)

it works somehow if I comment iconv code but it will be much better if I can recode this part:

return Iconv.iconv('UTF-8//IGNORE', 'UTF-8', (s + ' ') ).first[0..-2]

without iconv. Maybe I can use String#encode here somehow?

cnd
  • 32,616
  • 62
  • 183
  • 313

4 Answers4

40

Iconv was deprecated (removed) in 1.9.3. You can still install it.

Reference Material if you unsure: 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 => "?")

API

Dane Balia
  • 5,271
  • 5
  • 32
  • 57
  • 14
    "deprecated (removed)" - lol, these two words have completely different meanings. – Hakanai May 05 '14 at 12:09
  • [The `Iconv` page on APIdock](https://apidock.com/ruby/Iconv) also says that it's deprecated: "Even if some extentions [sic] of implementation dependent are useful, DON’T USE those extentions [sic] in libraries and scripts to widely distribute. If you want to use those feature, use String#encode." – GDP2 Feb 01 '19 at 23:49
9

String#scrub can be used since Ruby 2.1.

str.scrub(''),
str.scrub{ |bytes| '' }

Related question: Equivalent of Iconv.conv(“UTF-8//IGNORE”,…) in Ruby 1.9.X?

Community
  • 1
  • 1
masakielastic
  • 4,540
  • 1
  • 39
  • 42
8

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.

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. 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. (If you don't you'll corrupt any characters that aren't in that encoding - 7bit ASCII would be a really bad choice!)

David Waller
  • 3,057
  • 4
  • 27
  • 28
0

I have not had luck with the various approaches using a one line string.encode by itself

But I wrote a backfill that implements String#scrub in MRI pre 2.1, or other rubies that do not have it.

https://github.com/jrochkind/scrub_rb

jrochkind
  • 22,799
  • 12
  • 59
  • 74