Ruby:ARGV 破坏重音字符
# 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"
为什么会发生这种情况,如何让 ARGV 返回“Résumé”?
我已经设置了 chcp 65001 并正在使用 ruby 1.9.2p290 (2011-07-09) [i386-mingw32]
编辑 询问后在 irc 上,我被指示执行 chcp 1252>NUL 来解决问题。
# 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于某种原因,Windows 在您的控制台中不使用 UTF-8。因此,尽管 Ruby 需要 UTF-8 编码的字符串,但它会得到 Windows-1252 编码的字符串。
所以你有几种可能性(我无法测试,因为幸运的是,我不使用 Windows):
示例:
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):
chcp
should work and, if so, why it doesn't.Example: