2011/10/29 Damin M. Gonzlez <gonzalezdamianm / hotmail.com>:
> 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 tosee
> 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..
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 Rex