Skip to content

Instantly share code, notes, and snippets.

@carpeliam
Created December 27, 2009 22:47
Show Gist options
  • Save carpeliam/264428 to your computer and use it in GitHub Desktop.
Save carpeliam/264428 to your computer and use it in GitHub Desktop.
def ordinalize(i)
words = ['zeroth', 'first', 'second', 'third', 'fourth', 'fifth', 'sixth', 'seventh', 'eighth', 'ninth', 'tenth', 'eleventh', 'twelth', 'thirteenth', 'fourteenth', 'fifteenth', 'sixteenth', 'seventeenth', 'eighteenth', 'nineteenth']
deca_prefixes = {20 => 'twenty', 30 => 'thirty', 40 => 'forty', 50 => 'fifty', 60 => 'sixty', 70 => 'seventy', 80 => 'eighty', 90 => 'ninety'}
return words[i] unless i >= words.size
suffix = i % 10
prefix = i - suffix
return deca_prefixes[prefix].chop.concat('ieth') if suffix == 0
return deca_prefixes[prefix] + '-' + words[suffix]
end
(0...100).each {|i| puts ordinalize(i)}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment