How to replace the Unicode gem on Ruby 1.9?

后端 未结 2 1818
忘掉有多难
忘掉有多难 2020-12-14 04:59

Unfortunately, the Unicode 0.1 (sudo gem install unicode) doesn\'t work on Ruby 1.9. I have the following snippet:

require \"rubygems\"
require          


        
相关标签:
2条回答
  • 2020-12-14 05:41

    There is also I18n.transliterate('string') method in Rails. Works like a charm.

    0 讨论(0)
  • 2020-12-14 05:44

    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"
    

    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 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.

    0 讨论(0)
提交回复
热议问题