Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby and Accented Characters

Summary of the wall of text below: How can I display accented characters (so they work via puts, etc) in Ruby?


Hello! I am writing a program for my class which 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 accented characters in Java fine) or in the Command Prompt).

At first, some of my code didn't even run because the accented characters in my arrays where throwing off the Ruby interrupter (I guess?). I got errors like Ruby was expecting a closing bracket.

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 regardless of this line. But I needed to add this line to get my program to run successfully in Command Prompt. (I don't know why.)

I'm still, however, having a problem actually displaying the characters to the screen. A word such as "será" will display in the NetBeans output window as "seré". And in the command prompt it draws little pipe characters (that I don't know how to type).

Doing some more research, I heard about:

$KCODE = 'UTF-8'

but I'm not having any luck with this.


I'm using Ruby 1.8 and 1.9 (I go back and forth between different machines).

Thanks, Derek

like image 796
Derek Avatar asked Apr 10 '11 20:04

Derek


People also ask

What is an accented character?

Accent marks are symbols used over letters, commonly vowels, to help emphasize the pronunciation of a word. Accent marks are commonly found in languages such as French, German, Italian, and Spanish.

Are accented characters considered special characters?

Special characters are those beyond the standard 26 letter Roman alphabet, numerals 0 to 9 and the following characters: & (ampersand), ˚ (degree) and % (percentage). Characters beyond those listed above, such as letters with accents (é, è, ö, etc.)


1 Answers

A command prompt in Windows 7 has raster fonts by default. And it doesn't support unicode. At first, you should change cmd font to Lucida Console or Consolas. And then change the command prompt's codepage with chcp 65001. You can do it manually or add this line to your ruby programm:

# encoding: utf-8
`chcp 65001` #change cmd encoding to unicode
puts 'será test '
like image 171
Vasiliy Ermolovich Avatar answered Sep 26 '22 20:09

Vasiliy Ermolovich