Skip to content

Instantly share code, notes, and snippets.

@tlehman
Created March 4, 2012 22:43
Show Gist options
  • Save tlehman/1975194 to your computer and use it in GitHub Desktop.
Save tlehman/1975194 to your computer and use it in GitHub Desktop.
Integer patch: ordinal indicator
# Gives a string for the suffix of the ordinal number
# example usage:
# 2.suffix => "nd"
# 11.suffix => "th"
# 21.suffix => "st"
#
# TODO: Figure out what to do about 101?
# by tlehman: Sun 03/04/2012
class Integer
def suffix
# all the oddball suffixes
sufxs = {1=>"st", 2=>"nd", 3=>"rd"}
lastd = self % 10 # gets the last digit of the number
# notice that there are three exceptions to this rule,
# namely, eleventh, twelfth, and thirteenth. For all other numbers
# congruent to 1,2 or 3 (modulo 10), we use the oddball suffixes
if [11,12,13].include? self # this must be evaluated first, lest we get 11-st
suf = "th"
elsif sufxs.keys.include? lastd
suf = sufxs[lastd]
else
suf = "th"
end
return suf
end
def ordinal_indicator
return suffix
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment