3

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:\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>'
4

3 回答 3

9

这个文件是什么格式的?你确定它是 UTF-8 而不是Windows 1252是 Windows 的默认值吗?

在 Ruby 1.9 中,文件中的标题需要指明使用的实际格式:

# encoding: UTF-8

如果这不起作用,您可能需要与其他人进行试验:

# encoding: Windows-1252

另一种常见格式是ISO Latin1

# encoding: ISO-8859-1

1252 和 8859-1 都是单字节字符集,每个字符总是一个字节,其中 UTF-8 是可变长度,每个字符是一个或多个字节。

如果您需要在格式之间进行转换,通常您可以在具有编码意识的编辑器中打开并使用您想要的编码“另存为...”。否则,您可以尝试使用iconv为您转换它。

于 2013-07-25T14:27:22.110 回答
0

将这两行放在脚本的顶部。

#!/bin/env ruby
# encoding: utf-8
于 2013-07-25T14:27:01.937 回答
0

添加

# -*- encoding : utf-8 -*-

在文件的顶部

于 2013-07-25T13:36:40.203 回答