class String

Public Instance Methods

to_slug() click to toggle source

Create a custom method to convert strings to Slugs

# File lib/octocore/utils.rb, line 60
def to_slug
  #strip the string
  ret = self.strip

  #blow away apostrophes
  ret.gsub!(/['`]/,'')

  # @ --> at, and & --> and
  ret.gsub!(/\s*@\s*/, ' at ')
  ret.gsub!(/\s*&\s*/, ' and ')

  #replace all non alphanumeric, underscore or periods with underscore
  ret.gsub!(/\s*[^A-Za-z0-9\.\-]\s*/, '_')

  #convert double underscores to single
  ret.gsub!(/_+/,'_')

  #strip off leading/trailing underscore
  ret.gsub!(/\A[_\.]+|[_\.]+\z/,'')

  ret
end