From 33cf8f162e1ea2c10e6ba253b333f256802c3250 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 17 Jul 2005 10:02:23 +0000 Subject: 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] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1852 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../lib/active_support/core_ext/fixnum/inflections.rb | 15 +++++++++++++++ activesupport/lib/active_support/inflector.rb | 15 ++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 activesupport/lib/active_support/core_ext/fixnum/inflections.rb (limited to 'activesupport/lib') 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 -- cgit v1.2.3