As we know we can use Iconv
in Ruby 1.9.3 with TRANSLIT
flag which will replace accented characters with their ASCII equivalents, only if they're not present in destination encoding.
Example of use:
require 'iconv'
z = "Håkan"
Iconv.conv("windows-1250//TRANSLIT", "UTF-8", z)
# => outputs "Hakan" (with diactric removed)
pl = "zażółć"
Iconv.conv("windows-1250//TRANSLIT", "UTF-8", pl)
# => outputs "zażółć" (because windows-1250 contains all this characters)
# well, to be honest it outputs "za\xBF\xF3\xB3\xE6" because of terminal settings
# but I hope you understand
However Iconv
is deprecated and it's recommended to use String#encode
instead.
However when using #encode
the problem arises:
z.encode('windows-1250', 'utf-8')
Encoding::UndefinedConversionError: U+00E5 to WINDOWS-1250 in conversion from UTF-8 to WINDOWS-1250
Is there any way to get behavior similar to one with iconv
TRANSLIT
flag using String#encode
instead in Ruby 2+?