Module: Ordinalize

Included in:
Numeric
Defined in:
lib/ordinalize.rb

Instance Method Summary collapse

Instance Method Details

#conversionsObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ordinalize.rb', line 29

def conversions
  {
    "one" => "first",
    "two" => "second",
    "three" => "third", 
    "four" => "fourth",
    "five" => "fifth", 
    "six" => "sixth",
    "seven" => "seventh", 
    "eight" => "eighth",
    "nine" => "ninth", 
    "ten" => "tenth",
    "eleven" => "eleventh", 
    "twelve" => "twelveth",
  }
end

#ordinalizeObject Also known as: ordinalise



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/ordinalize.rb', line 7

def ordinalize
  humanized = self.humanize
  # 13-19
  humanized += "th" if humanized =~ /teen$/
  
  # 20, 30, 40, etc.
  humanized.gsub!(/ty$/, 'tieth')
  
  output = conversions[humanized] if conversions[humanized]
  output ||= split if split != ""
  output ||= humanized
end

#splitObject



22
23
24
25
26
27
# File 'lib/ordinalize.rb', line 22

def split
  humanized = self.humanize
  parts = humanized.split("-")
  parts[-1] = conversions[parts.last]
  parts.join("-")
end