Breno Salgado
Breno Salgado

Reputation: 1962

How can I get the equivalent non-utf8 chars from a utf8 string?

ruby 1.8.6, rails 2.3.8

Im doing some evals to write some methods to a class, and its working out nice(first time playing w/ metaprogramming :p), except that some strings that need to go into the methods(as code, and not strings) have accented characters and etc...

for example, I need to get "restricao" from the string "restrição", how can I do that, please?

Upvotes: 2

Views: 252

Answers (2)

Mirko
Mirko

Reputation: 5286

Check Unidecode gem!

"restrição".to_ascii #=> "restricao"

Upvotes: 2

Tonttu
Tonttu

Reputation: 1821

Use Iconv with TRANSLIT-feature, for example:

require 'iconv'
Iconv.conv("ASCII//TRANSLIT", "UTF-8", "restrição") == "restricao"

Upvotes: 0

Related Questions