Skip to content

Instantly share code, notes, and snippets.

@luigi
Created October 16, 2009 18:30
Show Gist options
  • Save luigi/211955 to your computer and use it in GitHub Desktop.
Save luigi/211955 to your computer and use it in GitHub Desktop.
# Adapted from ActiveSupport's parameterize
# http://github.com/rails/rails/blob/ea0e41d8fa5a132a2d2771e9785833b7663203ac/activesupport/lib/active_support/inflector.rb#L259
def slugify(str, sep = '-')
str.gsub!(/[^a-z0-9\-_\+]+/i, sep)
unless sep.nil? || sep == ''
re_sep = Regexp.escape(sep)
str.gsub!(/#{re_sep}{2,}/, sep)
str.gsub!(/^#{re_sep}|#{re_sep}$/i, '')
end
str.downcase
end
puts slugify("Hello World!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment