Skip to content

Instantly share code, notes, and snippets.

@cesarfigueroa
Last active September 7, 2017 03:29
Show Gist options
  • Save cesarfigueroa/2389328 to your computer and use it in GitHub Desktop.
Save cesarfigueroa/2389328 to your computer and use it in GitHub Desktop.
Ordinal Indicators in Ruby
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
@chapolito
Copy link

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

@cesarfigueroa
Copy link
Author

Good catch!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment