Class: String

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

Instance Method Summary collapse

Instance Method Details

#ellipsisize(minimum_length = 4, edge_length = 3) ⇒ Object



13
14
15
16
17
18
# File 'lib/x_tensions/string.rb', line 13

def ellipsisize(minimum_length=4,edge_length=3)
  return self if self.length < minimum_length or self.length <= edge_length*2
  edge = '.' * edge_length
  mid_length = self.length - edge_length*2
  gsub(/(#{edge}).{#{mid_length},}(#{edge})/, '\1...\2')
end

#humanizeObject



3
4
5
6
# File 'lib/x_tensions/string.rb', line 3

def humanize
  result = self.to_s.dup
  result.gsub(/_/, " ").capitalize
end

#shortizeObject



28
29
30
# File 'lib/x_tensions/string.rb', line 28

def shortize
  self.words.first.downcase
end

#to_slugObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/x_tensions/string.rb', line 32

def to_slug
ret = self.strip
ret.gsub! /['`]/,""
ret.gsub! /\s*@\s*/, " at "
ret.gsub! /\s*&\s*/, " and "
ret.gsub! /\s*\.\.\.\s*/, "  "
ret.gsub! /\s*\.\s*/, " dot "
ret.gsub! /\s*\#\s*/, " hash "
ret.gsub! /\s*\+\s*/, " plus "
ret.gsub! /\s*[^A-Za-z0-9\.\-]\s*/, '-'
ret.gsub! /-+/,"-"
ret.gsub! /\A[-\.]+|[-\.]+\z/,""
ret.downcase!

{
  }[ret] || ret
end

#to_utf8Object



8
9
10
11
# File 'lib/x_tensions/string.rb', line 8

def to_utf8
  self.encode!('UTF-16', 'UTF-8', :invalid => :replace, :replace => '')
  self.encode!('UTF-8', 'UTF-16')
end

#urlizeObject



24
25
26
# File 'lib/x_tensions/string.rb', line 24

def urlize
  self.strip.downcase.gsub(/[^a-z]+/, '_')
end

#wordsObject



20
21
22
# File 'lib/x_tensions/string.rb', line 20

def words
  self.split(/\s+/)
end