Skip to content

Instantly share code, notes, and snippets.

@havenwood
Last active September 27, 2015 06:17
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 havenwood/1224791 to your computer and use it in GitHub Desktop.
Save havenwood/1224791 to your computer and use it in GitHub Desktop.
ordinalize based on active_support's
require 'minitest/autorun'
require 'minitest/pride'
class Integer
def ordinalize
suffix = if (abs % 100).between? 11, 13
'th'
else
case abs % 10
when 1; 'st'
when 2; 'nd'
when 3; 'rd'
else 'th'
end
end
"#{self}#{suffix}"
end
alias_method :to_ord, :ordinalize
end
class TestOrdinalization < MiniTest::Unit::TestCase
def test_single_digit_numbers
assert_equal "-1st", -1.to_ord
assert_equal "1st", 1.to_ord
assert_equal "2nd", 2.to_ord
assert_equal "3rd", 3.to_ord
assert_equal "4th", 4.to_ord
assert_equal "5th", 5.to_ord
end
def test_oddball_cases
assert_equal "11th", 11.to_ord
assert_equal "12th", 12.to_ord
assert_equal "13th", 13.to_ord
assert_equal "-13th", -13.to_ord
end
def test_double_and_triple_digit_numbers
assert_equal "21st", 21.to_ord
assert_equal "55th", 55.to_ord
assert_equal "102nd", 102.to_ord
assert_equal "113th", 113.to_ord
assert_equal "999th", 999.to_ord
assert_equal "-999th", -999.to_ord
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment