Format a date like 1st, 2nd, 3rd

Source: Helper to print numbers as ordinals (1st, 2nd, 3rd...) [dzone.com]

class Fixnum
  def ordinal
    self.to_s + ( (10...20).include?(self) ? 'th' : %w{ th st nd rd th th th th th th }[self % 10] )
  end
end

d = Time.now.day
puts 'today is the ' + d.ordinal
#=> today is the 10th

Tags:
Source:
0702hrs.txt
Published:
08-12-2013 07:02