instance method ordinal

Ruby on Rails 4.2.9

Since v4.0.13

Available in: v4.0.13 v4.1.16 v4.2.9 v5.2.8.1 v6.0.6 v6.1.7.10 v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

ordinal(number)

Returns the suffix that should be added to a number to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th.

ordinal(1)     # => "st"
ordinal(2)     # => "nd"
ordinal(1002)  # => "nd"
ordinal(1003)  # => "rd"
ordinal(-11)   # => "th"
ordinal(-1021) # => "st"

Parameters

number req
Source
# File activesupport/lib/active_support/inflector/methods.rb, line 321
    def ordinal(number)
      abs_number = number.to_i.abs

      if (11..13).include?(abs_number % 100)
        "th"
      else
        case abs_number % 10
          when 1; "st"
          when 2; "nd"
          when 3; "rd"
          else    "th"
        end
      end
    end

Defined in activesupport/lib/active_support/inflector/methods.rb line 321 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveSupport::Inflector

Type at least 2 characters to search.

↑↓ navigate · open · esc close