Ruby and accented characters

A summary table of text below: How can I display accented characters (so that they work through puts, etc.) in Ruby?


Hello! I am writing a program for my class that will display some sentences in Spanish. When I try to use accented characters in Ruby, they do not display correctly (in the NetBeans output window (which displays characters with an accent in Java in order) or on the command line).

At first, some of my code didn’t even run, because the accented characters in my arrays were throwing the Ruby breaker (I suppose?). I got errors like Ruby was expecting parentheses to close.

But I did some research and found a solution to add the following line of code to the beginning of my Ruby file:

# coding: utf-8

In NetBeans, my program ran independently of this line. But I needed to add this line so that my program runs successfully on the command line. (I do not know why.)

I still have a problem displaying characters on the screen. The word "será" will appear in the NetBeans output window as "seré". And on the command line, he draws small channel characters (which I don’t know how to type).

After doing some more research, I heard about:

$KCODE = 'UTF-8'

but i'm out of luck with that.


I use Ruby 1.8 and 1.9 (I go back and forth between different machines).

Thanks Derek

+6
source share
1 answer

The command line in Windows 7 has bitmap fonts by default. And it does not support unicode. First you must change the font cmd to Lucida Console or Consolas . Then change the command line chcp 65001 using chcp 65001 . You can do this manually or add this line to your ruby ​​program:

 # encoding: utf-8 `chcp 65001` #change cmd encoding to unicode puts 'será test ' 
+3
source

Source: https://habr.com/ru/post/885621/


All Articles