2

Possible Duplicate:
How do I replace accented Latin characters in Ruby?

Is there an easy way to convert any letter that is not equal to a-z to a-z?

I want for example convert Ü to U, Ö to O and so on, I dont care about upper and lower case letters.

This is what I've so far.

{"ä" => "a", "å" => "a", "ö" => "o"}.each do |from, to|
  string.gsub!(/#{from}/i, to)
end

But I don't want to specify every word.

Any ideas?

Community
  • 1
  • 1
Linus Oleander
  • 17,181
  • 13
  • 67
  • 99
  • I take that comment half-way back. Apparently that solution over there uses a Rails method. – Joey Apr 05 '11 at 16:19

2 Answers2

2

Use the tr method.

 string.tr!( "äåö", "aao" );
tvanfosson
  • 509,016
  • 97
  • 693
  • 791
  • I guess it's way easier to use Unicode normalization and just strip out diacritics. – Joey Apr 05 '11 at 15:39
2

For a more general solution than String#tr, look at the stringex gem. http://github.com/rsl/stringex

Rein Henrichs
  • 15,142
  • 1
  • 43
  • 54