Ruby兼容性错误编码 [英] Ruby compatibility error encoding
问题描述
C:\temp> ruby script.rb
script.rb => Powershell输出
putsę=>呃#irb \xA9
把\xA9=> ▯
putsę==\xA9=> false
input = $ stdin.gets.chomp => input ==ę
putse#{input} e=> eęe
putsę== input => false
putsę#{input}=> Encoding ::兼容性错误Utf8& CP852
irb => #command line in ruby
putsę==\xA9=> true
input = $ stdin.gets.chomp => input ==ę
putsę== input =>真实&& \xA9== input => true
putsę#{input}=>噢嗯
看起来powershell的输入使用所有特殊字符的其他字体,而不是ruby和notepad ++(?)。我可以更改,所以它会工作,当我输入提示(当被问及)并没有显示错误?
编辑:对不起,误导。我添加了调用并指定该文件扩展名为.rb不是.txt
Edit2:好的,我研究了一些更多的信息,我一直在尝试一些编码(UTF8)到一个变量。发生了一些奇怪的事情
putsę#{input.encoding}=> ęCP852
将\xA9=> UTF-8
编码到CP852已经显示编码传递字节。我知道ę= 20 + 99 = 119,ą= 20 + 85,20 = C4
的值。得到它.encoding - 显示我使用的编码。这解决了这个问题。
putsę#{input.encodeUTF-8}=>嗯,
感谢大家的输入。
如果您输入的提示符(cmd或powershell)由于使用不同编码的不兼容而导致问题,请尝试使用脚本 .encode中的方法进行编码UTF-8#在Ruby语言的情况下
如果你不知道什么方法只是google your_language_name encoding
I'm having a problem. Let's look:
C:\temp> ruby script.rb
script.rb => Powershell output
puts "ę" => ę #irb \xA9
puts "\xA9" => ▯
puts "ę"=="\xA9" => false
input = $stdin.gets.chomp => input=="ę"
puts "e#{input}e" => eęe
puts "ę"==input => false
puts "ę#{input}" => Encoding::Compatibility Error Utf8 & CP852
irb => #command line in ruby
puts "ę"=="\xA9" => true
input = $stdin.gets.chomp => input=="ę"
puts "ę"==input => true && "\xA9"==input => true
puts "ę#{input}" => ęę
It looks like powershell's input uses other font for all special characters than ruby and notepad++(?). Can i change that so it will work when i type in prompt(when asked) and does not show an error?
Edit: Sorry for misdirection. I added invoke and specified that file has extension ".rb" not ".txt"
Edit2: Ok, I've researched some more information and I've been trying do some encoding(UTF8) to a variable. Somethin' strange occured.
puts "ę#{input.encoding}" => ęCP852
puts "\xA9" => UTF-8
Encoding to CP852 has revealed that encoding pass on bytes. I learned that value of "ę"=20+99=119, "ą" = 20 + 85, 20 = C4
Ok. got it ".encoding" - shows what encoding i use. And that resolve this problem.
puts "ę#{input.encode "UTF-8"}" => ęę
Thanks everyone for your input.
If your input in prompt( either cmd or powershell) is causing problems due to incompatibility of using differents encodings just try to encode it via methods in script.encode "UTF-8" #in case of Ruby language
If you dont know what methods do that just google your_language_name encoding
这篇关于Ruby兼容性错误编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!