diff options
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/active_support/core_ext/fixnum/inflections.rb | 15 | ||||
-rw-r--r-- | activesupport/lib/active_support/inflector.rb | 15 |
2 files changed, 29 insertions, 1 deletions
diff --git a/activesupport/lib/active_support/core_ext/fixnum/inflections.rb b/activesupport/lib/active_support/core_ext/fixnum/inflections.rb new file mode 100644 index 0000000000..13c4f1130a --- /dev/null +++ b/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 diff --git a/activesupport/lib/active_support/inflector.rb b/activesupport/lib/active_support/inflector.rb index 88f56403c8..be25a93687 100644 --- a/activesupport/lib/active_support/inflector.rb +++ b/activesupport/lib/active_support/inflector.rb @@ -60,6 +60,19 @@ module Inflector 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 def uncountable_words #:doc %w( equipment information rice money species series fish ) @@ -121,4 +134,4 @@ module Inflector [/s$/i, ''] ] end -end
\ No newline at end of file +end |