require 'delegate'
class OrdinalNumber < DelegateClass(Fixnum)
def to_s
delegate_s = __getobj__.to_s
check = abs
if check == 11 or check == 12
suffix = "th"
else
case check % 10
when 1 then suffix = "st"
when 2 then suffix = "nd"
else suffix = "th"
end
end
delegate_s + suffix
end
end
4.to_s # => "4"
OrdinalNumber.new(4).to_s # => "4th"
OrdinalNumber.new(102).to_s # => "102nd"
OrdinalNumber.new(11).to_s # => "11th"
OrdinalNumber.new(-21).to_s # => "-21st"
OrdinalNumber.new(5).succ # => 6
OrdinalNumber.new(5) + 6 # => 11
OrdinalNumber.new(5) + OrdinalNumber.new(6) # => 11
|