class Medal
include Comparable
attr_reader :titel, :time
def initialize(titel, time)
unless time.is_a?(Numeric)
raise ArgumentError.new("Time must be a numeric")
end
@titel = titel
@time = time
end
def <=>(medal)
return time <=> medal.time
end
end
__END__
class TestError < StandardError; end
bronze = Medal.new('Bronze', 180)
bronze2 = Medal.new('Bronze', 180)
bronze3 = Medal.new('Bronze3', 180)
silver = Medal.new('Silver', 105)
gold = Medal.new('Gold', 87.5)
unless (bronze == bronze2)
raise TestError.new("Medal.== does not compare #{bronze.inspect} " +
"and #{bronze2.inspect} properly")
end
unless (bronze == bronze3)
raise TestError.new("Medal.== does not ignore the title")
end
unless (silver <=> bronze) == -1
raise TestError.new("Medal.<=> does not return -1 when its time is less" +
" than the medal compared to")
end
unless (silver <=> gold) == 1
raise TestError.new("Medal.<=> does not return 1 when its time is greater" +
" than the medal compared to")
end
unless (bronze <=> gold) == 1
raise TestError.new("Medal.<=> does not preserve transitivity.\n" +
"I.e. <=> is highly problematic.")
end
p 'Medal testing completed successfully'
If you for any reason want to contact me you can email me at vobbys [AT] gmail <DOT> com