Skip to content

Instantly share code, notes, and snippets.

@bradland
Created February 13, 2013 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bradland/4945599 to your computer and use it in GitHub Desktop.
Save bradland/4945599 to your computer and use it in GitHub Desktop.
# 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