Created
February 13, 2013 15:57
-
-
Save bradland/4945599 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: UTF-8 | |
require 'rubygems' | |
require 'bundler/setup' | |
require 'active_support/inflector' | |
require 'iconv' | |
puts %[ | |
This is why String#encode does not replace Iconv, and neither | |
does ActiveSupport::Inflector#transliterate: | |
] | |
# This is our test string with some non-ASCII chars | |
string = %[This is a “smart quoted string”. This us an ümlaut. Weßia.] | |
puts %[ | |
String#encode as UTF-8 (this is redundant; it's our base string) | |
#{string.encode("UTF-8", :invalid => :replace, :undef => :replace, :replace => "?")} | |
Transliterate to ASCII using Iconv | |
#{Iconv.new('ASCII//TRANSLIT', 'UTF-8').iconv(string)} | |
Transliterate using ActiveSupport::Inflector | |
#{ActiveSupport::Inflector.transliterate(string)} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment