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.