_ruby topics on annotation usage and Chinese coding in Ruby language

Source: Internet
Author: User

Ruby annotation
Note hides a row for the Ruby interpreter, or part of a line, or several rows. You can use the character (#) at the beginning of the line:

# I'm a note, please ignore me.


Alternatively, the annotation can follow the same line as the statement or expression:

Name = "Madisetti" # This is also a comment

You can annotate multiple lines, as follows:

# This is a note.
# This is also a comment.
# This is also a comment.
# This is still a note.

Here is another form. This block annotation hides the line between =begin/=end for the interpreter:

=begin
This is a comment.
This is also a comment.
This is also a comment.
This is still a comment.
=end

Ruby Chinese Code
with Ruby output "Hello, world!", English is no problem, but if you output Chinese characters "Hello, the World" may encounter Chinese coding problems.
If no encoding is specified in the Python file, an error occurs during execution:

#!/usr/bin/ruby-w

puts "Hello, world!" ";

The results of the above program output are:

 
 


The above error message shows that Ruby uses ASCII code to read the source code, Chinese will appear garbled, the solution is as long as the beginning of the file to join the #-*-Coding:utf-8-*-(emac) or #coding =utf-8 on the line.
Instance

#!/usr/bin/ruby-w
#-*-coding:utf-8-*-

puts "Hello, world!" ";

The output results are:

Hello, world!


So if you are learning the process, the source code file, if the inclusion of Chinese code, you need to pay attention to two points:
1. You must add the #-*-Coding:utf-8-*-in the first line, telling the interpreter to use Utf-8 to parse the source.
2. You must set the editor to save the file encoded as 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.