Explanation of the annotation usage and Chinese Encoding Problems in the Ruby language.

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.