Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
563 views
in Technique[技术] by (71.8m points)

invalid multibyte char (UTF-8) Error, Ruby

I tried to run an ruby script in rails with the command rails runner. The ruby file, looks something like this and should create new patients:

 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:Siteswhat>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>'
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

What format is this file in? Are you sure it's UTF-8 and not Windows 1252 as is the default in Windows?

In Ruby 1.9, the header in your file needs to indicate the actual formatting used:

# encoding: UTF-8

If that doesn't work, you may need to experiment with others:

# encoding: Windows-1252

Another common format is 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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...