Last Updated: February 25, 2016
·
734
· javierg

Remove special chars with String#tr

Sometimes you need cleaning data containing unicode characters.
One easy way to do it is with the strings tr method.

puts "Hola áéíóú".tr('áéíóú', 'aeiou') #> "Hola aeiou"

This is very simple and you can evan have the character mappings in an array:

char_maps = %w/áéíóú aeiou/
puts "Hola áéíóú".tr(*char_maps) #> "Hola aeiou"

Enjoy!

Ref: http://ruby-doc.org/core-2.0/String.html#method-i-tr