From: Thomas Wieczorek Date: 2007-08-02T17:33:01+09:00 Subject: Re: Is there a standard "assert" idiom in Ruby? 2007/8/2, Kenneth McDonald : > I'm not talking anything quite so complex as design by contract, just > the difference between > > if x != y > ...raise new exception... > end > > vs. > > assert x == y > I am quite sure you're talking about Unit Testing. The PickAxe book has a good and concise chapter(chapter 12) about it. roman.rb class Roman MAX_ROMAN = 4999 def initialize(value) if value <= 0 or value > MAX_ROMAN then fail "Roman values must be > 0 and <= #{MAX_ROMAN}" end @value = value end FACTORS = [["m", 1000], ["cm", 900], ["d", 500], ["cd", 400], ["c", 100], ["xc", 90], ["l", 50], ["xl", 40], ["c", 10], ["ix", 9], ["v", 5], ["iv", 4], ["i", 1]] def to_s value = @value roman = "" for code, factor in FACTORS count, value = value.divmod(factor) roman << code unless count.zero? end roman end end test_roman.rb require 'roman' require 'test/unit' class TestRoman < Test::Unit::TestCase def test_simple assert_equal("i", Roman.new(1).to_s) #will throw a failure #assert_equal("xi", Roman.new(9).to_s) assert_equal("ix", Roman.new(9).to_s) assert_raise(RuntimeError) { Roman.new(5000) } end end http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html lists all available assert_* methods.