English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In den vorherigen Kapiteln haben wir gelernt, wie man mit Ruby "Hallo, Welt!" ausgibt, und das Englische funktioniert einwandfrei, aber wenn Sie chinesische Zeichen "你好,世界" ausgeben, könnte es zu chinesischen Kodierungsproblemen kommen.
If the encoding is not specified in the Ruby file, an error will occur during execution:
#!/usr/bin/ruby -w puts "Hello, World!";
The output result of the above program execution is:
invalid multibyte char (US-(ASCII)
The above error information shows that Ruby uses ASCII encoding to read source code, Chinese will appear garbled, the solution is to add # -*- coding: UTF-8 -*-(EMAC writing) or #coding=utf-8 is enough.
#!/usr/bin/ruby -w # -*- coding: UTF-8 -*- puts "Hello, World!";
The output result is:
Hello, World!
So if everyone in the process of learning, if the source code file contains Chinese encoding, then pay attention to two points: