Ruby 1.9:如何正确地对&小写的多字节字符串? [英] Ruby 1.9: how can I properly upcase & downcase multibyte strings?

查看:102
本文介绍了Ruby 1.9:如何正确地对&小写的多字节字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,马茨决定将红宝石1.9.1中的upcasedowncase限制为/[A-Z]/i.

So matz made the decision to keep upcase and downcase limited to /[A-Z]/i in ruby 1.9.1.

ActiveSupport::Multibyte长期以来一直通过String#mb_chars在ruby 1.8.x中微妙地进行i18n弹跳.

ActiveSupport::Multibyte has long had great i18n case jiggering in ruby 1.8.x via String#mb_chars.

但是,当在ruby 1.9.1下尝试时,它似乎不起作用.这是我写的一个简单的测试脚本,以及得到的输出:

However, when tried under ruby 1.9.1, it doesn't seem to work. Here's a simple test script I wrote, along with the output I'm getting:

$ cat test.rb
# encoding: UTF-8

puts("@ #{RUBY_VERSION} " + (__ENCODING__ rescue $KCODE).to_s)
sd, su = "Iñtërnâtiônàlizætiøn", "IÑTËRNÂTIÔNÀLIZÆTIØN"
def ps(u, d, k); puts "%-30s:  %24s / %-24s" % [k, u, d] end
ps sd.upcase, su.downcase, "Plain ruby"

require 'rubygems'; require 'active_support'
ps sd.upcase, su.downcase, "With active_support"
ps sd.mb_chars.upcase.to_s, su.mb_chars.downcase.to_s, "With active_support mb_chars"

$ ruby -KU test.rb
@ 1.8.7 UTF8
Plain ruby                    :  IñTëRNâTIôNàLIZæTIøN / iÑtËrnÂtiÔnÀlizÆtiØn
With active_support           :  IñTëRNâTIôNàLIZæTIøN / iÑtËrnÂtiÔnÀlizÆtiØn
With active_support mb_chars  :  IÑTËRNÂTIÔNÀLIZÆTIØN / iñtërnâtiônàlizætiøn

$ ruby1.9 test.rb
@ 1.9.1 UTF-8
Plain ruby                    :      IñTëRNâTIôNàLIZæTIøN / iÑtËrnÂtiÔnÀlizÆtiØn
With active_support           :      IñTëRNâTIôNàLIZæTIøN / iÑtËrnÂtiÔnÀlizÆtiØn
With active_support mb_chars  :      IñTëRNâTIôNàLIZæTIøN / iÑtËrnÂtiÔnÀlizÆtiØn

那么,如何使用ruby 1.9.1将upcasedowncase国际化?

So, how do I get internationalized upcase and downcase with ruby 1.9.1?

我应该补充一点,我还通过GitHub上当前的master2-3-*3-0-unstable rails分支对ActiveSupport进行了测试.结果相同.

I should add that I also tested with ActiveSupport from the current master, 2-3-* and 3-0-unstable rails branches at GitHub. Same results.

推荐答案

大小写转换取决于语言环境,并且并不总是往返的,这就是为什么Ruby 1.9没有覆盖它的原因(请参见

Case conversion is locale dependent and doesn't always round-trip, which is why Ruby 1.9 doesn't cover it (see here and here)

unicode-util gem 应该可以满足您的需求.

The unicode-util gem should address your needs.

这篇关于Ruby 1.9:如何正确地对&小写的多字节字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆