Skip to content

Instantly share code, notes, and snippets.

@souzaonofre
Created June 22, 2016 18:48
Show Gist options
  • Save souzaonofre/0af93a8a2c8f774e2e9912a2d30f705a to your computer and use it in GitHub Desktop.
Save souzaonofre/0af93a8a2c8f774e2e9912a2d30f705a to your computer and use it in GitHub Desktop.
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