Forum: Ruby Problem when removing accents from a String

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
César D. (Guest)
on 2009-04-12 21:06

I've spend several time trying to replace latin characters in strings.
What I want is something like this:

Given the word "camión", I would like to get the following string:

I've found several solutions in the Internet but they only work in the
Rails console, and when I try to launch my Rails application on Mongrel
(I am using Netbeans) nothing is working and from "camión" I am getting
"camin" instead of "camion".

For example, I've tried with these snippets:

def nice_slug(str)

    accents = {
      ['á','à','â','ä','ã'] => 'a',
      ['Ã','Ä','Â','À','Á'] => 'A',
      ['é','è','ê','ë'] => 'e',
      ['Ë','É','È','Ê'] => 'E',
      ['í','ì','î','ï'] => 'i',
      ['Í','Î','Ì','Ï'] => 'I',
      ['ó','ò','ô','ö','õ'] => 'o',
      ['Õ','Ö','Ô','Ò','Ó'] => 'O',
      ['ú','ù','û','ü'] => 'u',
      ['Ú','Û','Ù','Ü'] => 'U',
      ['ç'] => 'c', ['Ç'] => 'C',
      ['ñ'] => 'n', ['Ñ'] => 'N'
    accents.each do |ac,rep|
      ac.each do |s|
      str = str.gsub(s, rep)
    str = str.gsub(/[^a-zA-Z0-9 ]/,"")

    str = str.gsub(/[ ]+/," ")

    str = str.gsub(/ /,"-")

    str = str.downcase



It throws the following error:

undefined method `normalize' for "cami�n:String



It throws the following error:

undefined method `decompose' for "cami�n:String

I am using Ruby 1.8.6, Rails 2.2, Mongrel 1.1.5 and Windows XP.

Please, some help.
Axel E. (Guest)
on 2009-04-12 21:58
(Received via mailing list)
Dear César,

have you also tried iconv
( )?
A quick fix might be to use it to convert between different,
encodings, ignoring the accents (which occur in utf-8 or iso-8859-1
encoding, but not in the target ascii encoding).

I can't try it out on Windows XP right now, but you can see some
discussion and examples here:

Another option might be to use the HTML entities gem.
It gives you the names of the accented character, and you can then
remove the accents in a small routine using regexps,

require 'rubygems'
require 'htmlentities'
coder =
string = "<élan>"
res=coder.encode(string, :named)       # => "&lt;&eacute;lan&gt;"

and replace the name of the accent with a regexp:


This works well for the western European and northern European
but it fails for some accents of middle Eastern European languages, such
as the  Polish ogoneks ( and the
carons of Czech : ...

Best regards,

This topic is locked and can not be replied to.