1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Refactor Inflector#ordinal to avoid converting the number twice

This commit is contained in:
Carlos Antonio da Silva 2012-11-04 18:51:47 -02:00
parent 5cdeb5ef7d
commit ade6c74ba6

View file

@ -286,10 +286,12 @@ module ActiveSupport
# ordinal(-11) # => "th"
# ordinal(-1021) # => "st"
def ordinal(number)
if (11..13).include?(number.to_i.abs % 100)
abs_number = number.to_i.abs
if (11..13).include?(abs_number % 100)
"th"
else
case number.to_i.abs % 10
case abs_number % 10
when 1; "st"
when 2; "nd"
when 3; "rd"