# Difference in iconv between ruby and irb

**URL:** <https://www.ruby-forum.com/t/difference-in-iconv-between-ruby-and-irb/141683>\
**Category:** Ruby\
**Created:** [June 29, 2008, 9:57am UTC](https://www.ruby-forum.com/t/difference-in-iconv-between-ruby-and-irb/141683 "2008-06-29T09:57:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![edek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5a626/32.png) [@edek](https://www.ruby-forum.com/u/edek)\
**Post date:** [June 29, 2008, 9:57am UTC](https://www.ruby-forum.com/t/difference-in-iconv-between-ruby-and-irb/141683/1 "2008-06-29T09:57:18Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![edek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5a626/32.png) [@edek](https://www.ruby-forum.com/u/edek)\
**Post date:** [June 30, 2008, 12:28pm UTC](https://www.ruby-forum.com/t/difference-in-iconv-between-ruby-and-irb/141683/2 "2008-06-30T12:28:37Z")

</div>

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. 🙂

– Shot

---

<div class="post-metadata">

**Author:** ![edek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5a626/32.png) [@edek](https://www.ruby-forum.com/u/edek)\
**Post date:** [June 30, 2008, 1:01pm UTC](https://www.ruby-forum.com/t/difference-in-iconv-between-ruby-and-irb/141683/3 "2008-06-30T13:01:56Z")

</div>

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
