Module: ToCardinal

Defined in:
lib/to_cardinal/version.rb,
lib/to_cardinal/cardinalize.rb

Constant Summary collapse

VERSION =
"0.0.2"
EXPLICITS =
{
  'first'         => 1,
  'second'        => 2,
  'third'         => 3,
  'ninth'         => 9,
  'eleventh'      => 11,
  'twelfth'       => 12,
  'twentieth'     => 20,
  'thirtieth'     => 30,
  'fortieth'      => 40,
  'fiftieth'      => 50,
  'sixtieth'      => 60,
  'seventieth'    => 70,
  'eightieth'     => 80,
  'ninetieth'     => 90,
  'one hundredth' => 100
}
REGULARS =
{
  'thir'  => 3,
  'four'  => 4,
  'fif'   => 5,
  'six'   => 6,
  'seven' => 7,
  'eigh'  => 8,
  'nine'  => 9,
  'ten'   => 10
}
TENS =
{
  'twenty'  => 20,
  'thirty'  => 30,
  'forty'   => 40,
  'fifty'   => 50,
  'sixty'   => 60,
  'seventy' => 70,
  'eighty'  => 80,
  'ninety'  => 90
}
EXPLICIT_MATCHES =
Hash[REGULARS.map {|k, v| [k.to_s + 'th', v] }].merge EXPLICITS
TENS_MATCH =
/(#{TENS.keys.join '|'})-/
ORDINAL =
/^(\d+)(?:st|nd|rd|th)$/

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cardinalize(str) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/to_cardinal/cardinalize.rb', line 46

def self.cardinalize(str)
  str.downcase!

  ordinal          = str[ORDINAL, 1]
  explicit_matches = EXPLICIT_MATCHES[str]
  regular_match    = str[/^(.+)teenth$/, 1]

  return ordinal.to_i if ordinal
  return explicit_matches if explicit_matches
  return 10 + REGULARS[regular_match] if regular_match

  if tens = str[TENS_MATCH, 1]
    sum = TENS[tens]
    str.sub! "#{tens}-", ''
    EXPLICIT_MATCHES.has_key?(str) ? sum + EXPLICIT_MATCHES[str] : nil
  end
end

Instance Method Details

#to_cardinalObject



64
65
66
# File 'lib/to_cardinal/cardinalize.rb', line 64

def to_cardinal
  ToCardinal.cardinalize self
end