Ruby coding instructions

Source: Internet
Author: User

ProgramEncoding is generally divided into several situations:
1. source code file encoding
2. receive external Content Encoding
3. Runtime Environment Code
4. Operating System Code

First, you can add a line of declaration in the header of the Ruby file to encode the source code file. In this way, all the characters in the source code are saved as the specified encoding:

 
#-*-Coding: UTF-8 -*-

When receiving content or reading external files, you may not be sure about the encoding. At this time, you can transcode the externally received characters or content.

 
String. encode ('utf-8') # convert string to UTF-8 mode require "NKF" string = NKF. NKF ("-W", string) # convert the string to UTF-8. The value of-W represents UTF-8.

The runtime environment encoding, which is usually the same as the operating system encoding by default, can also be set to the specified encoding, so that all encoding consistency is maintained. Set the runtime environment encoding method in Ruby as follows: add the encoding parameter and specify the encoding format when you start Ruby.

 
Ruby-encoding = UTF-8

The operating system code is usually GBK in Chinese. Therefore, if you use the value in the system in a program, you need to pay attention to it. For example, the Directory of the Chinese name; if your runtime environment is UTF-8, You Need To transcode the path string into GBK before reading the files in the Chinese directory. Otherwise, an error is reported.

Here is a problem:
Ruby X:/path/to/Ruby/Chinese/file. RB # The RB file can be found successfully. The runtime environment is encoded as GBK.
Ruby -- encoding = UTF-8 X:/path/to/Ruby/Chinese/file. RB # The RB file cannot be found, and an Encoding Error is prompted because the runtime environment is UTF-8 and the path parameter passed in is GBK encoding.
If the runtime environment is GBK and the source code is UTF-8 encoded, the system will prompt that the encoding is incompatible when processing strings such as Chinese in some places, in some cases, the default encoding for character encoding is the runtime environment encoding.
Ruby X:/path/to/Ruby/english/file. RB # You can find it. The runtime environment is the same as the source code.

SoThe runtime environment encoding should have the following priority: whether the encoding parameter is specified ==> the character encoding format when non-Chinese characters are processed for the first time

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.