Explanation of the annotation usage and Chinese Encoding Problems in the Ruby language.
Ruby Annotation
The annotation hides a line, a part of a line, or several lines from the Ruby interpreter. You can use the following characters at the beginning of the line (#):
# I am a comment. Ignore me.
Alternatively, the comment can be followed by the same line of the statement or expression:
Name = "Madisetti" # this is also a comment
You can annotate multiple rows as follows:
# This is a comment. # This is also a comment. # This is also a comment. # This is a comment.
The following is another form. This block annotation will hide the rows between = begin/= end for the interpreter:
= Begin this is a comment. This is also a comment. This is also a comment. This is a comment. = End
Ruby Chinese Encoding
Output "Hello, World! ", English is correct, but if you output Chinese characters" Hello, world ", you may encounter Chinese Encoding Problems.
If no encoding is specified in the Python file, an error occurs during execution:
#! /Usr/bin/ruby-wputs "Hello, world! ";
The output result of the above program execution is:
invalid multibyte char (US-ASCII)
The above error message shows that Ruby uses ASCII encoding to read the source code, and garbled characters may appear in Chinese. The solution is to add #-*-coding at the beginning of the file: UTF-8-*-(EMAC) or # coding = UTF-8.
Instance
#! /Usr/bin/ruby-w #-*-coding: UTF-8-*-puts "Hello, world! ";
Output result:
Hello, world!
Therefore, if the source code file contains Chinese encoding, pay attention to the following two points:
1. You must add #-*-coding: UTF-8-*-in the first line to tell the interpreter to use UTF-8 to parse the source code.
2. You must set the encoding of the file saved in the editor to UTF-8.