Skip to content

Instantly share code, notes, and snippets.

@mblythe86
Forked from alterisian/fixnum_ordinalize.rb
Last active March 9, 2016 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mblythe86/bc41a8940fdf0d9301a7 to your computer and use it in GitHub Desktop.
Save mblythe86/bc41a8940fdf0d9301a7 to your computer and use it in GitHub Desktop.
Adds ordinalize i.e. th,st,rd to a number for use in date string outputs
class Fixnum
def ordinal
abs_number = self.abs
if (11..13).include?(abs_number % 100)
"th"
else
case abs_number % 10
when 1; "st"
when 2; "nd"
when 3; "rd"
else "th"
end
end
end
def ordinalize
"#{self}#{ordinal()}"
end
end
@mblythe86
Copy link
Author

Adapted from this rails code

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