Skip to content

Instantly share code, notes, and snippets.

@natritmeyer
Created January 6, 2012 14:59
Show Gist options
  • Save natritmeyer/1570951 to your computer and use it in GitHub Desktop.
Save natritmeyer/1570951 to your computer and use it in GitHub Desktop.
Ruby monkeypatch: Integer#position_suffix
# 1.position_suffix => "st"
# 2.position_suffix => "nd"
# 3.position_suffix => "rd"
# 4.position_suffix => "th"
# 11.position_suffix => "th"
# 21.position_suffix => "st"
# 345678.position_suffix => "th"
class Integer
def position_suffix
if (11..13).include? (self % 100)
"th"
else
case to_s[-1]
when "1" then "st"
when "2" then "nd"
when "3" then "rd"
else "th"
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment