[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Difference in iconv between ruby and irb

edek

6/29/2008 7:51:00 AM

Hi,

I have simple script:

require 'iconv'
s = '?e?an?' # 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,

--
Marcin
1 Answer

Daniel Lucraft

6/30/2008 10:59:00 AM

0

Shot (Piotr Szotkowski) 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
--
Posted via http://www.ruby-....