Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如果使用以下字符保存文本文件б U+0431,但将其保存为 ANSI 代码页文件。
б
U+0431
红宝石返回ord = 63。在代码页返回时使用 UTF-8 保存文件ord = 208, 177
ord = 63
ord = 208, 177
我是否应该特别告诉 Ruby 处理使用特定代码页编码的输入?如果是这样,你如何做到这一点?
是在 ruby 源代码中还是在读取的文件中File.open?如果它在 ruby 源代码中,您可以(在 ruby 1.9 中)将其添加到文件顶部:
File.open
# encoding: utf-8
或者您可以指定大多数其他编码(如 iso-8859-1)。
如果您正在读取带有 的文件File.open,则可以执行以下操作:
File.open("file.txt", "r:utf-8") {|f| ... }
与编码注释一样,您也可以在此处传递不同类型的编码。