Ruby兼容性错误编码

时间:2016-05-04 13:55:29

标签: ruby powershell encoding special-characters

我遇到了问题。我们来看看:

  

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}"          => ęę

对于所有特殊字符而言,powershell的输入使用其他字体而不是ruby和notepad ++(?)。我可以更改它,以便当我输入提示(当被询问时)并且不显示错误时它会起作用吗?

编辑:抱歉误导。我添加了invoke并指定该文件的扩展名为“.rb”而不是“.txt”
编辑2:好的,我已经研究了一些更多的信息,我一直在尝试对变量进行一些编码(UTF8)。有些奇怪的发生了。

puts "ę#{input.encoding}"         => ęCP852
puts "\xA9"                       => UTF-8

编码到CP852已经发现编码传递字节。我了解到“ę”的值= 20 + 99 = 119,“±”= 20 + 85,20 = C4

确定。得到它“.encoding” - 显示我使用的编码。这解决了这个问题。

puts "ę#{input.encode "UTF-8"}"         => ęę

感谢大家的意见。

1 个答案:

答案 0 :(得分:0)

如果您在提示符输入(cmd或powershell)导致由于使用不同编码的不兼容而导致问题,请尝试通过脚本.encode "UTF-8" #in case of Ruby language中的方法对其进行编码。如果您不知道哪些方法只是google {{ 1}}