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

Refactor ActiveSupport::Inflector#ordinal #8117

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions activesupport/lib/active_support/inflector/methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -286,15 +286,10 @@ def safe_constantize(camel_cased_word)
# ordinal(-11) # => "th"
# ordinal(-1021) # => "st"
def ordinal(number)
if (11..13).include?(number.to_i.abs % 100)
"th"
if number.to_i.abs % 100 / 10 != 1 && (1..3).include?(last_digit = number.to_i.abs % 10)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be overkill to create a private method teen?(number)? to make it immediately obvious what that first bit is doing?

{1 => "st", 2 => "nd", 3 => "rd"}[last_digit]
else
case number.to_i.abs % 10
when 1; "st"
when 2; "nd"
when 3; "rd"
else "th"
end
"th"
end
end

Expand Down