Removing accents in a Ruby String

With Ruby on Rails, it often seems the developers thought of everything because there’s a ton of helpers to do most common tasks. One thing that’s clearly missing though is a way to remove accents from a string. I’m not talking about escaping characters, I’m talking about converting the accented characters to their base letter (i.e. “éàû” should become “eau“).

I’ve spent a bit of time today thinking about the best way to do this. Once again, Ruby’s awesome flexibility means we can actually extend the standard String class and add a method to it. In a Ruby on Rails project, we add this library to the lib directory, add one line to the environment.rb file and we’re good to go.

Head over to the Remove Accents From A Ruby String script page to see all the details and to download the script you can use in your Ruby (and Rails) projects. With it, you’ll be able to simply do:

"été".removeaccents

Yes, that simple. I’ve also added another method to prepare a string to be used as part of a URL. Rather than escaping characters, it converts them using removeaccents and it calls a few additional methods to make sure the string is ready.

"été".urlize

We’ve published the script under a creative commons licence. Use it, abuse it, modify it and make sure to send back your changes so we can have a great version to share with everyone.

Tags: , , ,

Please leave a comment on this post