简体   繁体   中英

Does Ruby auto-detect a file's codepage?

If a save a text file with the following character б U+0431 , but save it as an ANSI code page file.

Ruby returns ord = 63 . Saving the file with UTF-8 as the codepage returns ord = 208, 177

Should I be specifically telling Ruby to handle the input encoded with a certain code page? If so, how do you do this?

Is that in ruby source code or in a file which is read with File.open ? If it's in the ruby source code, you can (in ruby 1.9) add this to the top of the file:

# encoding: utf-8

Or you could specify most other encodings (like iso-8859-1).

If you are reading a file with File.open , you could do something like this:

File.open("file.txt", "r:utf-8") {|f| ... }

As with the encoding comment, you can pass in different types of encodings here too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM