From: Dan Zwell Date: 2007-08-02T17:13:53+09:00 Subject: Re: Is there a standard "assert" idiom in Ruby? Michael Fellinger wrote: > class AssertionError < StandardError > end > > def assert a, b > raise AssertionError, "#{a.inspect} != #{b.inspect}" unless a == b > end > > assert 1, 1 > assert 1, 2 > > I like this idea, but I would make it more general (so you could use it for more than equality tests): class AssertionError < StandardError end def assert(message=nil, &block) unless(block.call) raise AssertionError, (message || "Assertion failed") end end Then you can do: >> assert {3==3} => nil >> assert("This test will fail") {false} AssertionError: This test will fail from (irb):24:in `assert' from (irb):29 >> assert { 3>=1 } => nil And AssertionError will cause an exit (which is desirable behavior), unless it is caught. Dan