Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR #2072 with docs #4878

Merged
merged 2 commits into from Feb 7, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions activesupport/CHANGELOG.md
@@ -1,5 +1,7 @@
## Rails 4.0.0 (unreleased) ## ## Rails 4.0.0 (unreleased) ##


* Adds Integer#ordinal to get the ordinal suffix string of an integer. *Tim Gildea*

* AS::Callbacks: `:per_key` option is no longer supported * AS::Callbacks: `:per_key` option is no longer supported


* `AS::Callbacks#define_callbacks`: add `:skip_after_callbacks_if_terminated` option. * `AS::Callbacks#define_callbacks`: add `:skip_after_callbacks_if_terminated` option.
Expand Down
14 changes: 14 additions & 0 deletions activesupport/lib/active_support/core_ext/integer/inflections.rb
Expand Up @@ -14,4 +14,18 @@ class Integer
def ordinalize def ordinalize
ActiveSupport::Inflector.ordinalize(self) ActiveSupport::Inflector.ordinalize(self)
end end

# Ordinal returns the suffix used to denote the position
# in an ordered sequence such as 1st, 2nd, 3rd, 4th.
#
# 1.ordinal # => "st"
# 2.ordinal # => "nd"
# 1002.ordinal # => "nd"
# 1003.ordinal # => "rd"
# -11.ordinal # => "th"
# -1001.ordinal # => "st"
#
def ordinal
ActiveSupport::Inflector.ordinal(self)
end
end end
34 changes: 24 additions & 10 deletions activesupport/lib/active_support/inflector/methods.rb
Expand Up @@ -250,6 +250,29 @@ def safe_constantize(camel_cased_word)
end end
end end


# Returns the suffix that should be added to a number to denote the position
# in an ordered sequence such as 1st, 2nd, 3rd, 4th.
#
# Examples:
# ordinal(1) # => "st"
# ordinal(2) # => "nd"
# ordinal(1002) # => "nd"
# ordinal(1003) # => "rd"
# ordinal(-11) # => "th"
# ordinal(-1021) # => "st"
def ordinal(number)
if (11..13).include?(number.to_i.abs % 100)
"th"
else
case number.to_i.abs % 10
when 1; "st"
when 2; "nd"
when 3; "rd"
else "th"
end
end
end

# Turns a number into an ordinal string used to denote the position in an # Turns a number into an ordinal string used to denote the position in an
# ordered sequence such as 1st, 2nd, 3rd, 4th. # ordered sequence such as 1st, 2nd, 3rd, 4th.
# #
Expand All @@ -261,16 +284,7 @@ def safe_constantize(camel_cased_word)
# ordinalize(-11) # => "-11th" # ordinalize(-11) # => "-11th"
# ordinalize(-1021) # => "-1021st" # ordinalize(-1021) # => "-1021st"
def ordinalize(number) def ordinalize(number)
if (11..13).include?(number.to_i.abs % 100) "#{number}#{ordinal(number)}"
"#{number}th"
else
case number.to_i.abs % 10
when 1; "#{number}st"
when 2; "#{number}nd"
when 3; "#{number}rd"
else "#{number}th"
end
end
end end


private private
Expand Down
5 changes: 5 additions & 0 deletions activesupport/test/core_ext/integer_ext_test.rb
Expand Up @@ -22,4 +22,9 @@ def test_ordinalize
assert_equal '1st', 1.ordinalize assert_equal '1st', 1.ordinalize
assert_equal '8th', 8.ordinalize assert_equal '8th', 8.ordinalize
end end

def test_ordinal
assert_equal 'st', 1.ordinal
assert_equal 'th', 8.ordinal
end
end end
6 changes: 6 additions & 0 deletions activesupport/test/inflector_test.rb
Expand Up @@ -303,6 +303,12 @@ def test_safe_constantize
end end


def test_ordinal def test_ordinal
OrdinalNumbers.each do |number, ordinalized|
assert_equal(ordinalized, number + ActiveSupport::Inflector.ordinal(number))
end
end

def test_ordinalize
OrdinalNumbers.each do |number, ordinalized| OrdinalNumbers.each do |number, ordinalized|
assert_equal(ordinalized, ActiveSupport::Inflector.ordinalize(number)) assert_equal(ordinalized, ActiveSupport::Inflector.ordinalize(number))
end end
Expand Down
17 changes: 16 additions & 1 deletion railties/guides/source/active_support_core_extensions.textile
Expand Up @@ -1872,9 +1872,24 @@ The method +multiple_of?+ tests whether an integer is multiple of the argument:


NOTE: Defined in +active_support/core_ext/integer/multiple.rb+. NOTE: Defined in +active_support/core_ext/integer/multiple.rb+.


h4. +ordinal+

The method +ordinal+ returns the ordinal suffix string corresponding to the receiver integer:

<ruby>
1.ordinal # => "st"
2.ordinal # => "nd"
53.ordinal # => "rd"
2009.ordinal # => "th"
-21.ordinal # => "st"
-134.ordinal # => "th"
</ruby>

NOTE: Defined in +active_support/core_ext/integer/inflections.rb+.

h4. +ordinalize+ h4. +ordinalize+


The method +ordinalize+ returns the ordinal string corresponding to the receiver integer: The method +ordinalize+ returns the ordinal string corresponding to the receiver integer. In comparison, note that the +ordinal+ method returns *only* the suffix string.


<ruby> <ruby>
1.ordinalize # => "1st" 1.ordinalize # => "1st"
Expand Down