Skip to content

Commit

Permalink
Added Fixnum#ordinalize to turn 1.ordinalize to "1st", 3.ordinalize t…
Browse files Browse the repository at this point in the history
…o "3rd", and 10.ordinalize to "10th" and so on #1724 [paul@cnt.org]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1852 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
dhh committed Jul 17, 2005
1 parent 80cfa76 commit 33cf8f1
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 2 deletions.
5 changes: 5 additions & 0 deletions activesupport/CHANGELOG
@@ -1,3 +1,8 @@
*SVN*

* Added Fixnum#ordinalize to turn 1.ordinalize to "1st", 3.ordinalize to "3rd", and 10.ordinalize to "10th" and so on #1724 [paul@cnt.org]


*1.1.1* (11 July, 2005) *1.1.1* (11 July, 2005)


* Added more efficient implementation of the development mode reset of classes #1638 [Chris McGrath] * Added more efficient implementation of the development mode reset of classes #1638 [Chris McGrath]
Expand Down
15 changes: 15 additions & 0 deletions activesupport/lib/active_support/core_ext/fixnum/inflections.rb
@@ -0,0 +1,15 @@
require File.dirname(__FILE__) + '/../../inflector'
module ActiveSupport #:nodoc:
module CoreExtensions #:nodoc:
module Fixnum #:nodoc:
module Inflections
# 1.ordinalize # => "1st"
# 3.ordinalize # => "3rd"
# 10.ordinalize # => "10th"
def ordinalize
Inflector.ordinalize(self)
end
end
end
end
end
15 changes: 14 additions & 1 deletion activesupport/lib/active_support/inflector.rb
Expand Up @@ -60,6 +60,19 @@ def constantize(camel_cased_word)
end end
end end


def ordinalize(number)
if (11..13).include?(number.to_i % 100)
"#{number}th"
else
case number.to_i % 10
when 1: "#{number}st"
when 2: "#{number}nd"
when 3: "#{number}rd"
else "#{number}th"
end
end
end

private private
def uncountable_words #:doc def uncountable_words #:doc
%w( equipment information rice money species series fish ) %w( equipment information rice money species series fish )
Expand Down Expand Up @@ -121,4 +134,4 @@ def singular_rules #:doc:
[/s$/i, ''] [/s$/i, '']
] ]
end end
end end
39 changes: 38 additions & 1 deletion activesupport/test/inflector_test.rb
Expand Up @@ -131,6 +131,37 @@ class InflectorTest < Test::Unit::TestCase
"underground" => "Underground" "underground" => "Underground"
} }


OrdinalNumbers = {
"0" => "0th",
"1" => "1st",
"2" => "2nd",
"3" => "3rd",
"4" => "4th",
"5" => "5th",
"6" => "6th",
"7" => "7th",
"8" => "8th",
"9" => "9th",
"10" => "10th",
"11" => "11th",
"12" => "12th",
"13" => "13th",
"14" => "14th",
"20" => "20th",
"21" => "21st",
"22" => "22nd",
"23" => "23rd",
"24" => "24th",
"100" => "100th",
"101" => "101st",
"102" => "102nd",
"103" => "103rd",
"104" => "104th",
"110" => "110th",
"1000" => "1000th",
"1001" => "1001st"
}

def test_pluralize_plurals def test_pluralize_plurals
assert_equal "plurals", Inflector.pluralize("plurals") assert_equal "plurals", Inflector.pluralize("plurals")
assert_equal "Plurals", Inflector.pluralize("Plurals") assert_equal "Plurals", Inflector.pluralize("Plurals")
Expand Down Expand Up @@ -214,4 +245,10 @@ def test_constantize
assert_equal InflectorTest, Inflector.constantize("InflectorTest") assert_equal InflectorTest, Inflector.constantize("InflectorTest")
assert_raises(NameError) { Inflector.constantize("UnknownClass") } assert_raises(NameError) { Inflector.constantize("UnknownClass") }
end end
end
def test_ordinal
OrdinalNumbers.each do |number, ordinalized|
assert_equal(ordinalized, Inflector.ordinalize(number))
end
end
end

0 comments on commit 33cf8f1

Please sign in to comment.