views:

94

answers:

2

I'm just starting to learn Ruby and have a problem with encoding;

require 'rubygems'
require 'mechanize'
agent = Mechanize.new
agent.get('myurl.....')
agent.page.search('#reciperesult a').each do |item|
   c = Mechanize.new
   c.get(item.attributes['href'])
   puts c.page.search('#ingredients li').text
end

The output text are shown like this h├©nsekj├©tt when it should have been shown like this hønsekjøtt. I'm using Ruby 1.8.7. Can anybody point me in the right direction?

A: 

Try adding:

$KCODE ='UTF8'

at the beginning of your script.

EDIT: seems someone else had an encoding problem before. Try to follow the advice given here

Geo
I tried that, and it didn't help :-(
Fossmo
+2  A: 

Where are you viewing the output? Is it on the console? If so, are you running Windows XP? I have found that on my Windows setup the console (I do my development in the Git+ terminal) it does not show non-ascii characters correctly. On the terminal on my Mac they show fine.

If you suspect that this is the problem then write the output to a file and then view that file in a text editor. This should show you the correct output.

sosborn
Yeah. `cmd.exe` does not output the characters correctly. Also, if you write to a file, make sure you use the right encoding.
Geo
Thanks, that solved the problem :-)
Fossmo