[英]invalid multibyte char (UTF-8) Error, Ruby
I tried to run an ruby script in rails with the command rails runner.我尝试使用命令 rails runner 在 rails 中运行 ruby 脚本。 The ruby file, looks something like this and should create new patients:
ruby 文件看起来像这样,应该会创建新患者:
Patient.create!({:vorname => 'Josepha', :nachnahme => 'Brecht', :geburtsdatum => '25.04.1963', :strasse => 'Umdorf', :ort => 'Wörthss', :plz => '93093'})
Patient.create!({:vorname => 'Tumba', :nachnahme => 'Hoch', :geburtsdatum => '17.77.1956', :strasse => 'Hamaß 1', :ort => 'Brenn', :plz => '93189'})
But somehow my code has problems with the german language!但不知何故,我的代码与德语有问题! Im programming beginner and do not know what i have to change!
我是编程初学者,不知道我必须改变什么! Thanks for help!
感谢帮助!
C:\Sites\what>rails runner patienten.rb
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/c
ommands/runner.rb:51:in `eval': patienten.rb:2: invalid multibyte char (UTF-8) (
SyntaxError)
patienten.rb:2: syntax error, unexpected tIDENTIFIER, expecting '}'
...> 'Schlossberg', :ort => 'Wörth', :plz => '93086'})
... ^
patienten.rb:2: syntax error, unexpected tINTEGER, expecting $end
...:ort => 'Wörth', :plz => '93086'})
... ^
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.1
3/lib/rails/commands/runner.rb:51:in `<top (required)>'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.1
3/lib/rails/commands.rb:64:in `require'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.1
3/lib/rails/commands.rb:64:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
What format is this file in? 该文件的格式是什么? Are you sure it's UTF-8 and not Windows 1252 as is the default in Windows?
您确定它是UTF-8而不是Windows 1252,因为它是Windows中的默认设置吗?
In Ruby 1.9, the header in your file needs to indicate the actual formatting used: 在Ruby 1.9中,文件中的标头需要指明所使用的实际格式:
# encoding: UTF-8
If that doesn't work, you may need to experiment with others: 如果这不起作用,则可能需要与其他人一起尝试:
# encoding: Windows-1252
Another common format is ISO Latin1 : 另一种常见格式是ISO Latin1 :
# encoding: ISO-8859-1
Both 1252 and 8859-1 are single-byte character sets, each character is always one byte, where UTF-8 is variable length, each character is one or more bytes. 1252和8859-1均为单字节字符集,每个字符始终为一个字节,其中UTF-8为可变长度,每个字符为一个或多个字节。
If you need to convert between formats, usually you can open in an editor that's encoding aware and "Save As..." with the encoding you want. 如果需要在格式之间进行转换,通常可以在一个支持编码的编辑器中打开,并使用所需的编码“另存为...”。 Otherwise you might try using iconv to convert it for you.
否则,您可以尝试使用iconv为您进行转换。
Add 加
# -*- encoding : utf-8 -*-
at the top of the file 在文件的顶部
put these two line at the top of the script. 将这两行放在脚本的顶部。
#!/bin/env ruby
# encoding: utf-8
irb(main):088:0> "hi\x99!".encode("UTF-8", "Windows-1252")
=> "hi™!"
Justin Weiss has a great article about encoding in Ruby. Justin Weiss 有一篇关于 Ruby 编码的精彩文章。 https://www.justinweiss.com/articles/3-steps-to-fix-encoding-problems-in-ruby/
https://www.justinweiss.com/articles/3-steps-to-fix-encoding-problems-in-ruby/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.