English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In the previous chapters, we have learned how to output "Hello, World!" in Ruby, which is fine in English, but if you output Chinese characters "你好,世界", you may encounter Chinese encoding issues.
If the encoding is not specified in the Ruby file, an error will occur during execution:
#!/usr/bin/ruby -w puts "Hello, World!";
The above program execution output result is:
invalid multibyte char (US-(ASCII)
The above error information shows that Ruby uses ASCII encoding to read source code, and Chinese characters will appear garbled. The solution is to add # -*- coding: UTF-8 -*-(EMAC notation) 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 is learning, in the source code file, if it contains Chinese encoding, then you need to pay attention to two points: