Created
June 22, 2016 18:48
-
-
Save souzaonofre/0af93a8a2c8f774e2e9912a2d30f705a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def make_slug(str) | |
return if str.blank? or !str.instance_of? String | |
s = str | |
s.downcase! | |
s.strip! | |
s.gsub!(/[^a-z0-9\s-]/, '') # Remove non-word characters | |
s.gsub!(/\s+/, '-') # Convert whitespaces to dashes | |
s.gsub!(/-\z/, '') # Remove trailing dashes | |
s.gsub!(/-+/, '-') # get rid of double-dashes | |
s.to_s | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment