问题描述
很遗憾,Unicode 0.1 (sudo gem install unicode
) 不适用于 Ruby 1.9.我有以下片段:
Unfortunately, the Unicode 0.1 (sudo gem install unicode
) doesn't work on Ruby 1.9. I have the following snippet:
require "rubygems"
require "unicode"
str = "áéíóúç"
Unicode.normalize_KD(str).gsub(/[^x00-x7F]/n, "")
#=> aeiouc
我使用它将标题转换为永久链接,而不删除重音字符.
I use it to convert titles to permalink, without removing accented characters.
有没有办法使用 pack
或 unpack
方法转换这些文本?
Is there a way of converting such texts using pack
or unpack
methods?
推荐答案
更新: 更好的选择可能是使用 gem unicode_utils
专为这些缺失的功能而创建:
Update: a better option may be to use the gem unicode_utils
that was created specifically for these missing features:
require "unicode_utils"
UnicodeUtils.nfkd("áéíóúç").gsub(/[^x00-x7F]/,'').to_s
#=> "aeiouc"
是否有可能依赖 Rails 的 ActiveSupport?然后您可以执行以下操作:
Is there a possibility you can depend on Rails' ActiveSupport? Then you can do the following:
require "activesupport"
mb_str = ActiveSupport::Multibyte::Chars.new("áéíóúç")
mb_str.normalize(:kd).gsub(/[^x00-x7F]/,'').to_s
#=> "aeiouc"
ActiveSupport::Multibyte
被编写来为 Ruby 1.8 带来 UTF-8/Unicode 支持,但在 1.9 中也可以正常工作.你也许可以借用一些代码 如果您不希望它作为外部依赖项.
ActiveSupport::Multibyte
was written to bring UTF-8/Unicode support to Ruby 1.8, but works fine in 1.9 too. You may be able to borrow some of the code if you don't want it as an external dependency.
这篇关于如何在 Ruby 1.9 上替换 Unicode gem?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!