Skip to content

This is an extension to the Ruby String class that adds a few methods to remove the accents in a string and replace them with basic 7 bits english characters.

Notifications You must be signed in to change notification settings

jonathangrenier/Remove-Accents-Ruby-Script

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

Removing Accents In a Ruby String

In short, you can use this extension to the String class to convert "àéêîôûù" to "aeeiouu".

It's fairly easy to convert a string to a URL-safe version. Both PHP and Ruby have a way to do this, but what happens in this case is that extended characters are escaped rather than being replaced and that's often not what I want.

If you're like me, you've probably had many occasions where you wanted to get a 7bits ASCII version of a string where the accents are converted to the base letter. For example, you want to give each user his page at : [domain.com]/[name]. When the name is "Steve Smith", that's not a problem, but what if the name is "François Léveillé" ?

What you want in this case is "francois_leveille" or "francoisleveille" and this is where this script will help you.

First, DOWNLOAD THE SCRIPT. Make sure to require it in your project. If you are using Ruby on Rails, place it in the lib directory and add this line to the bottom of your config/environment.rb file:

require 'extend_string'

Once you're done, you now have 2 additional methods in the String class. This means that for any string anywhere in your project, you can do:

Example use

# Set a sample string to test things out
mystring = "Ceci Est UN test : éàòù"

# The removeaccents method simple removes the accents and returns the string
mystring.removeaccents

# The urlize method not only calls removeaccents, 
# but also a bunch of others to make it truly URL-ready.
mystring.urlize

# You can customize urlize with options:
# :downcase => true
#     will convert entire string to lowercase
# :convert_spaces => true
#     will convert space to underscore
# :regexp => //
#     matching characters will be removed

mystring.urlize({:downcase => true})

About

This is an extension to the Ruby String class that adds a few methods to remove the accents in a string and replace them with basic 7 bits english characters.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages