From ade6c74ba6b5f10a923cd42564adfeddecbc4364 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Sun, 4 Nov 2012 18:51:47 -0200 Subject: Refactor Inflector#ordinal to avoid converting the number twice --- activesupport/lib/active_support/inflector/methods.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/inflector/methods.rb b/activesupport/lib/active_support/inflector/methods.rb index 3910a2dc42..1eb2b4212b 100644 --- a/activesupport/lib/active_support/inflector/methods.rb +++ b/activesupport/lib/active_support/inflector/methods.rb @@ -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" -- cgit v1.2.3