Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/core_ex/string.rb

Instance Method Summary collapse

Instance Method Details

#to_slugObject



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/core_ex/string.rb', line 2

def to_slug
  slug = self.downcase.strip

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

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

  #replace all non alphanumeric to dash
  #slug.gsub!(/\W+/, ' ')
  slug.gsub!(/\s*[^a-z0-9]\s*/, '-')

  #Replace spaces with dashes
  slug.gsub!(' ', '-')

  #convert double dash to single
  slug.gsub!(/-+/, '-')

  #strip off leading/trailing dash
  slug.gsub!(/\A[-\.]+|[-\.]+\z/, '')

  slug
end