views:

115

answers:

6

Is there any better alternative to this?

name.gsub('è','e').gsub('à','a').gsub('ò','o').gsub('ì','i').gsub('ù','u')

thanks

+1  A: 
substitutes = {'è'=>'e', 'à'=>'a', 'ò'=>'o', 'ì'=>'i', 'ù'=>'u'}
substitutes.each do |old, new| 
    name.gsub!(old, new)
end

Or you could use an extension of String such as this one to do it for you.

Kaleb Brasee
+7  A: 

Use tr.

Maybe like string.tr('èàòìù', 'eaoiu').

Anonymous
+1  A: 

If you really want a full solution, try pulling the tables from Perl's Unidecode module. After translating those tables to Ruby, you'll want to loop over each character of the input, substituting the table's value for that character.

eswald
A: 

Taking a wild stab in the dark, but if you're trying to remove the accented characters because you're using a legacy text encoding format you should look at Iconv.

An introduction which is great on the subject: http://blog.grayproductions.net/articles/encoding_conversion_with_iconv

ba
A: 

In case you are wondering the technical terms for what you want to do is Case Folding and possibly Unicode Normalization (and sometimes collation).

Here is a case folding configuration for ThinkingSphinx to give you an idea of how many characters you need to worry about.

srboisvert
A: 

If JRuby is an option, see the answer to my question:

http://stackoverflow.com/questions/1673544/how-do-i-detect-unicode-characters-in-a-java-string

It deals with removing accents from letters, using a Normalizer. You could access that class from JRuby.

Geo