Permalink
Browse files

Refactor Inflector#ordinal to avoid converting the number twice

  • Loading branch information...
1 parent 5cdeb5e commit ade6c74ba6b5f10a923cd42564adfeddecbc4364 @carlosantoniodasilva carlosantoniodasilva committed Nov 4, 2012
Showing with 4 additions and 2 deletions.
  1. +4 −2 activesupport/lib/active_support/inflector/methods.rb
@@ -286,10 +286,12 @@ def safe_constantize(camel_cased_word)
# 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"

0 comments on commit ade6c74

Please sign in to comment.