如何取代Ruby 1.9上的Unicode宝石? [英] How to replace the Unicode gem on Ruby 1.9?

查看:111
本文介绍了如何取代Ruby 1.9上的Unicode宝石?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不幸的是,Unicode 1.9( 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 was was编写为将UTF-8 / Unicode支持带到Ruby 1.8,但在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宝石?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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