Msgstr "Invalid multibyte char (US-ASCII) error" for ä, ü, ö, ß which are Ascii!

My application must handle some international characters, namely ä, ü, ö and ß, which are still ascii.

When I tested the ruby ​​behavior when working with these characters, I got this error:

test.rb:1: invalid multibyte char (US-ASCII)
test.rb:1: invalid multibyte char (US-ASCII)

for this code:

puts "i like my chars: ä, ü, ö and ß!"

But strange: when using Interactive Ruby Shell I do not get errors!

EDIT: in my application, I am extracting data from an external api. The above code is just an example!

+3
source share
6 answers

Put a magical comment # coding: utf-8at the beginning of your script (in the second line if you use shebang).

#!/usr/local/bin/ruby
# coding: utf-8

puts "i like my chars: ä, ü, ö and ß!"
+8
source

, ASCII. ASCII Unicode U + 007F ( 127). . wikipedia ASCII.

, Ruby , ASCII.

.rb ? , , ASCII.

+11

ASCII... , ASCII. , , UTF-8, , ASCII, .

, .

+1

ASCII, escape- Unicode:

puts "i like my chars: \u00E4, \u00FC, \u00F6 and \u00DF!"

Ruby 1.9, - , 1.8.

0

, shebang script ruby19 macosx:

GreyJewel:Moralytics atma$ head -n1 moralytics 
**# coding: utf-8**
GreyJewel:Moralytics atma$ which ruby
/opt/local/bin/ruby
GreyJewel:Moralytics atma$ ruby --version
ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin11]
GreyJewel:Moralytics atma$ ruby moralytics 
Το λεξικό που χρησιμοποιούμε έχει: 407117 λέξεις
Το λεξιλόγειο του χρήστη atmat εμπεριέχει: 3943 λέξεις συνολικά.

. shebang . , .

0

script, -ascii? script.

# encoding: utf-8

.

Or, if you want to make the project wide, you have the magic-encoding gem option

0
source

Source: https://habr.com/ru/post/1759678/


All Articles