Last active
September 7, 2017 03:29
-
-
Save cesarfigueroa/2389328 to your computer and use it in GitHub Desktop.
Ordinal Indicators in Ruby
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
class Fixnum | |
def with_ordinals | |
if (11..13).include?(self.abs % 100) | |
'th' | |
else | |
case self.abs % 10 | |
when 1 then 'st' | |
when 2 then 'nd' | |
when 3 then 'rd' | |
else 'th' | |
end | |
end.prepend(self.to_s) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks this helped, found this after some more digging:
if (11..13).include?(abs_number % 100)
so you get 111th instead of 111st http://apidock.com/rails/ActiveSupport/Inflector/ordinal