Usage and precautions of string transcoding IN RUBY

Source: Internet
Author: User

Unlike Java, Ruby does not support Unicode internally (it is said that Unicode is supported by 2.0, but not confirmed)
In character encoding, Ruby uses the iconv library of Open Group. This library appears to be available by default in Linux, but does not exist in Windows. The solution is at the end.

Using iconv is simple, the following is an example of converting iso8859-1 encoding to GBK Encoding

Require 'iconv'
Conv = iconv. New ("GBK", "ISO-8859-1 ")
Result = Conv. iconv ("hello ")
Result <Conv. iconv (nil)
Conv. Close

You need to call iconv (NiL) to refresh the data that has not been output after the conversion.

Block can be used to simplify the preceding method.

Iconv. Open (toencoding, fromencoding) Do | cd |
Input. Each {| S | output <CD. iconv (s )}
Output <CD. iconv (nil)
End

Of course, there is a simpler way

Iconv. Conv (toendocing, fromencoding, STR)

Finally, I would like to remind you that the iconv accept encoding is toencoding in front, and the fromencoding In The Back should not be mistaken. I am even depressed for a long time because of this problem.

ASIDE: Many Ruby functions depend on various libraries. For example, iconv is a typical example of the glue language.

--------------------------------------------------------------------
Supplement: Install iconv in Windows

Download the iconv installation package here and place the file in the package as follows.

Iconv. So-> Ruby \ Lib \ Ruby \ 1.8 \ i386-mswin32 \
Iconv. dll-> Windows \ system32 \
Charset. dll-> Windows \ system32 \

For more details, see readme in the installation package.

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.