Class: Numeric

Inherits:
Object
  • Object
show all
Defined in:
lib/eztime.rb

Overview

The following code was based on work found at: www.bigbold.com/snippets/user/jswizard#post2368

Instance Method Summary collapse

Instance Method Details

#ordinal(include_cardinal = true) ⇒ Object

Returns the cardinal (number) and ordinal (st, nd, rd, th, etc.) Pass include_cardinal as false to only return the ordinal



8
9
10
11
12
13
14
15
16
# File 'lib/eztime.rb', line 8

def ordinal(include_cardinal=true)
  cardinal = self.to_i.abs
  if (10...20).include?(cardinal) then
    include_cardinal ? cardinal.to_s << 'th' : 'th'
  else
    ord = %w{th st nd rd th th th th th th}[cardinal % 10]
    include_cardinal ? cardinal.to_s << ord : ord
  end
end