Encoding trouble

Hello friends, first i have to say: my english is not so good, I’ll try
my best. I passed many hours yesterday and a couple of hours today
trying to figure out why my source code isn’t displayed as supposed.
I’ll show you the code to continue:

1 # encoding: UTF-8
2 puts “¿Cual es tu nombre? áéíóúñ”
3 nombre = gets.chomp
4 puts “¿Cual es tu segundo nombre?”
5 segundonombre = gets.chomp
6 puts “¿Cual es tu apellido?”
7 apellido = gets.chomp
8 puts "Hola " + nombre + " " + segundonombre + " " + apellido +
9 “!! Un gustaso conocerte!! :)”

The trouble******
I save this program as something.rb, and then execute it with require
‘something’. I putted some characters like áéíóúñ¿ to show up in the
ruby prompt, is supposed that ruby will show me this: ¿Cual es tu
nombre? áéíóúñ, but Ruby show me crabbed characters instead of áéíóúñ¿.

A few things to say:

  1. I’m running Windows SP3 in my PC. I have used Komodo and InType
    (IDEs), and with both is the same result. I have tryied many ways: I
    saved the file with UTF-8, ISO-8859-1 and variants and always the same
    result.
  2. I’ve added puts ENCODING in the middle of the source code to see
    if ruby was getting the mean of the first line of the code, and it was
    good, Ruby gives: UTF-8, but still show me strange characters.
  3. I`ve re-instaled Ruby, old 1.9 versions and it the same, I don’t
    want to have to run Ruby 1.8, reading i found that this is a trouble of
    1.9 ver…
  4. Running the program:

require ‘anything’
┐┐Cual es tu nombre? ├í├®├¡├│├║├▒
=> Ñàácaró
┐┐Cual es tu segundo nombre?
=> Úúúlñ
┐┐Cual es tu apellido?
=> ¿¿¿¿¿¿ñá
Hola Ñàácaró Úúúlñ ¿¿¿¿¿¿ñá!! Un gustaso conocerte!! :slight_smile:

Observe that when I write in the prompt characters with ´ or ` or ¿

#or ñ Ruby take it fine, i mean, it don’t respond me with strange
#characters.

  1. Strange things that I’ve found in Ruby about this trouble:

puts ENCODING
=> CP850 # CP850 is the Latin-1 encoding
“ñññññ”
=> “\xA4\xA4\xA4\xA4\xA4”
puts “ñññññ”
=> ñññññ
“ñññññ”.force_encoding(“CP850”)
=> “\xA4\xA4\xA4\xA4\xA4”
“ñññññ”.force_encoding(“UTF-8”)
=> “\xA4\xA4\xA4\xA4\xA4”
“ñññññ”.force_encoding(“ISO-8859-1”) # ISO-8859-1 is the Latin-1
#encoding
=> “ñññññ”

I will be very grateful with your help, thanks.

2011/10/29 Damin M. Gonzlez [email protected]:

trying to figure out why my source code isn’t displayed as supposed.

  1. I’m running Windows SP3 in my PC

FWIW, your example displays just fine on my Mac (OS X 10.6.8) with
ruby-1.9.2-p290, so I suspect your problem is Windows-related.

Can’t help more on that, other than to suggest at least setting up a
Linux VM for development/testing…

2011/10/29 Damián M. González [email protected]:

  1. I’ve added puts ENCODING in the middle of the source code to see
    if ruby was getting the mean of the first line of the code, and it was
    good, Ruby gives: UTF-8, but still show me strange characters.
  2. I`ve re-instaled Ruby, old 1.9 versions and it the same, I don’t
    want to have to run Ruby 1.8, reading i found that this is a trouble of
    1.9 ver…

This has nothing to do with Ruby itself.

Windows’ command prompts is absolutely broken with regards to national
characters - it doesn’t support UTF-8, only a certain codepage,
different ones for different languages. You say you’re running
codepage 850, my computer (with Polish as main language) uses CP852.
(You can see it using chcp command in the cmd window.)

You should either use CP850 encoding for your code (bad idea, since
it’s not used anywhere except for Windows’ command prompt), or encode
all text before printing it, like this:

encoding: UTF-8

puts “¿Cual es tu nombre? áéíóúñ”.encode(‘cp850’)

As far as I know, all input from console will come encoded as CP850,
too, but Ruby might not realize it - you’ll probably want to reencode
it to UTF-8, if you’ll use it anywhere later or save to file, or don’t
and hope for the best ;).

nombre = gets.chomp
nombre = nombre.force_encoding(‘cp850’).encode(‘utf-8’)

(And, once again - I’m talking about CP850 above, but it will be
different on different computers and you might get funky characters
again if you for example send the code to your buddy in Turkey or
Russia.)

– Matma R.

Well, thank you guys for answer, I really apreciate it. I was one step
to install Linux once for all( not the first time with troubles with
Ruby-XP ), but i said “ey!, lets try one more thing!”, I tried in the
source code what u told me Rex:
“¿Cual es tu nombre? áéíóúñ”.encode(‘cp850’) …
and it works, in the prompt the characters with á, ñ and ¿ shows
correctly.
So I put # encoding: CP850 in the first line of the source code. I
didn’t tried that before because Komodo( IDE ) doesn’t have the CP850
option for encoding( but many others )…but…when I tried this way,
magicly (?(laughs)) Komodo encoded the source code in that codepage
(850) and the program work fine, characters showing correctly. In
conclusion, now I can see why people complain about Windows…is not for
programers( just my opinion ), meaby one day I’ll jump to some other OS
xD see you guys, and thank you very much.