Ruby 兼容性错误编码

Ruby compatibility error encoding

我有问题。让我们看看:

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++(?) 的其他字体。我可以更改它以便在我输入提示时(询问时)它会起作用并且不显示错误吗?

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

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

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

好的。知道了“.encoding” - 显示我使用的编码。这解决了这个问题。

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

感谢大家的意见。

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