Created
January 6, 2012 14:59
-
-
Save natritmeyer/1570951 to your computer and use it in GitHub Desktop.
Ruby monkeypatch: Integer#position_suffix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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