简体   繁体   English

Ruby:ARGV打破重音字符

[英]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" test.rb简历 >“R \\ xE9sum \\ xE9”

Why does this occur, and how can I get ARGV to return "Résumé"? 为什么会发生这种情况,我怎样才能让ARGV返回“Résumé”?

I have chcp 65001 set already and am using ruby 1.9.2p290 (2011-07-09) [i386-mingw32] 我已经设置了chcp 65001并使用了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. 编辑在询问irc之后,我被指示做chcp 1252>NUL修复了问题。

For some reason, Windows doesn't use UTF-8 in your console. 出于某种原因,Windows不在您的控制台中使用UTF-8。 So, although Ruby expects UTF-8 encoded string, it gets Windows-1252 encoded string. 因此,虽然Ruby期望UTF-8编码的字符串,但它获得了Windows-1252编码的字符串。

So you have several possibilities (which I can't test as I, fortunately, don't use Windows): 所以你有几种可能性(我不能测试,幸运的是,不要使用Windows):

  1. Persuade Windows to use UTF-8 in your console. 说服Windows在您的控制台中使用UTF-8。 I don't know if chcp should work and, if so, why it doesn't. 我不知道chcp是否应该工作,如果是的话,为什么不工作。
  2. Tell Ruby to use Windows-1252 instead of UTF-8 as default 告诉Ruby使用Windows-1252而不是UTF-8作为默认值
  3. Convert ARGV from Windows-1252 to UTF-8 manually: 手动将ARGV从Windows-1252转换为UTF-8:

Example: 例:

>> argument = "R\xE9sum\xE9"
=> "R\xE9sum\xE9"
>> argument.force_encoding('windows-1252').encode('utf-8')
=> "Résumé"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM