Module: Ordinalize

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

Instance Method Summary collapse

Instance Method Details

#conversionsObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ordinalize.rb', line 35

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
19
# 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_dashes if split_dashes != ""
  output ||= split_spaces if split_spaces != ""
  output ||= humanized
end

#split_dashesObject



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

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

#split_spacesObject



29
30
31
32
33
# File 'lib/ordinalize.rb', line 29

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