Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby: ARGV breaks accented characters

# encoding: utf-8
foo = "Résumé"
p foo

> "Résumé"

# encoding: utf-8
ARGV.each do |argument|
    p argument
end

test.rb Résumé > "R\xE9sum\xE9"

Why does this occur, and how can I get ARGV to return "Résumé"?

I have chcp 65001 set already and am using ruby 1.9.2p290 (2011-07-09) [i386-mingw32]

EDIT After asking around on irc, I was instructed to do chcp 1252>NUL which fixed the problem.

like image 687
SaulGoodman Avatar asked Jul 16 '26 10:07

SaulGoodman


1 Answers

For some reason, Windows doesn't use UTF-8 in your console. So, although Ruby expects UTF-8 encoded string, it gets Windows-1252 encoded string.

So you have several possibilities (which I can't test as I, fortunately, don't use Windows):

  1. Persuade Windows to use UTF-8 in your console. I don't know if chcp should work and, if so, why it doesn't.
  2. Tell Ruby to use Windows-1252 instead of UTF-8 as default
  3. Convert ARGV from Windows-1252 to UTF-8 manually:

Example:

>> argument = "R\xE9sum\xE9"
=> "R\xE9sum\xE9"
>> argument.force_encoding('windows-1252').encode('utf-8')
=> "Résumé"
like image 180
Mladen Jablanović Avatar answered Jul 17 '26 23:07

Mladen Jablanović



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!