Reputation: 1696
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>'
Upvotes: 3
Views: 15125
Reputation: 893
irb(main):088:0> "hi\x99!".encode("UTF-8", "Windows-1252")
=> "hi™!"
Justin Weiss has a great article about encoding in Ruby. https://www.justinweiss.com/articles/3-steps-to-fix-encoding-problems-in-ruby/
Upvotes: 1
Reputation: 211560
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.
Upvotes: 9
Reputation: 7225
put these two line at the top of the script.
#!/bin/env ruby
# encoding: utf-8
Upvotes: 0