虽然我还没有测试过,但是字符编码库(目前是 alpha 版本)向 String 类添加了处理 UTF-8 和其他字符的方法。它在 RubyForge 上的页面在这里。它是为 Ruby 1.8 设计的。
然而,根据我的经验,使用 Ruby 1.8,如果您将数据以 UTF-8 格式存储在数据库中,只要 HTTP 标头中的字符编码是 UTF-8,Ruby 就不会妨碍您。它可能无法对琴弦进行操作,但它不会破坏任何东西。例子:
file.txt:
¡Hola! ¿Como estás? Leí el artículo. ¡Fue muy excellente!
原谅我可怜的西班牙语;这是我能想到的最好的 Unicode 例子。
in irb:
str = File.read("file.txt")
=> "\302\241Hola! \302\277Como est\303\241s? Le\303\255 el art\303\255culo. \302\241Fue muy excellente!\n"
str += "Foo is equal to bar."
=> "\302\241Hola! \302\277Como est\303\241s? Le\303\255 el art\303\255culo. \302\241Fue muy excellente!\nFoo is equal to bar."
str = " " + str + " "
=> " \302\241Hola! \302\277Como est\303\241s? Le\303\255 el art\303\255culo. \302\241Fue muy excellente!\nFoo is equal to bar. "
str.strip
=> "\302\241Hola! \302\277Como est\303\241s? Le\303\255 el art\303\255culo. \302\241Fue muy excellente!\nFoo is equal to bar."
基本上,它只会将 UTF-8 视为带有奇数字符的 ASCII。如果代码点乱序,它不会按字典顺序排序;但是,它将按代码点排序。例子:
"\302" <=> "\301"
=> -1
无论如何,您打算对 Rails 应用程序中的数据进行多少操作?大多数排序等通常由您的数据库引擎完成。