Difference in iconv between ruby and irb

Hi,

I have simple script:

require ‘iconv’
s = ‘±ê¶æñ¼’ # a few polish letters with accents
puts Iconv.iconv(‘ASCII//TRANSLIT’, ‘UTF-8’, s)

I expect result like “aescnz”.

I run “ruby myscript.rb” and the effect is “???” - something went
wrong with the transliteration.

The case is, that if I run irb and write the same code, effect is OK.

What should I import to my script or where is the difference between
ruby interpreter and irb that makes it working in the second case?

Thanks in advance,

edek:

I have simple script:

require ‘iconv’
s = ‘ąęśćńź’ # a few polish letters with accents
puts Iconv.iconv(‘ASCII//TRANSLIT’, ‘UTF-8’, s)

I expect result like “aescnz”.

You need to tell Ruby you’re talking in UTF-8 to it in the first place.
I have -Ku in my $RUBYOPT environmental variable (you can also simply
$KCODE = ‘u’
in your Ruby code) and I get the below:

shot@devielle:~$ irb
require ‘iconv’
=> true

Iconv.iconv ‘ASCII//TRANSLIT’, ‘UTF-8’, ‘ąćęłńóśźż’
=> [“acelnoszz”]

BTW: Ładny adres email. :slight_smile:

– Shot

Shot (Piotr S.) wrote:

You need to tell Ruby you’re talking in UTF-8 to it in the first place.
I have -Ku in my $RUBYOPT environmental variable (you can also simply
$KCODE = ‘u’
in your Ruby code) and I get the below:

Sad to say but this is a problem that we have spent many hours trying to
fix. By all means, try fiddling with the the KCODE variables. You may
also want to experiment with setting the environment LANG and LC
variables, and importing parts of irb. Good luck with that.

When that doesn’t work you have two (maybe three) options.

First you can install Ruby-GNOME2, which has the function
GLib.convert/3. That works properly on our system even though Iconv
doesn’t:

require ‘gtk2’
GLib.convert(“ąćęłńóśźż”, “ASCII//translit”, “UTF-8”)

Second, you can call out to iconv through the shell:

%x{echo ‘ąćęłńóśźż’ | iconv -t ASCII//TRANSLIT}

though in that case you will have to deal with error messages and
whatnot, which can be a bit of a pain.

The third option is write your own wrapper for libiconv. This is what
are probably going to end up doing just to remove the RG2 dependency.

If anyone can come up with any better solutions, believe me I’m all
ears.

Specifically, the script:

$KCODE=‘u’
require ‘iconv’
p Iconv.iconv(‘ASCII//TRANSLIT’, ‘UTF-8’, ‘ąćęłńóśźż’)

when run with options:

ruby -Ku iconv.rb

produces:

["???"]

Whereas in irb:

$ irb
irb(main):001:0> require ‘iconv’
=> false
irb(main):002:0> Iconv.iconv ‘ASCII//TRANSLIT’, ‘UTF-8’, ‘ąćęłńóśźż’
=> [“acelnoszz”]

Any Ruby gurus out there who have any idea why this should be?

Dan