From: Ned Konz Date: 2002-06-03T11:23:56+09:00 Subject: Re: Does assert() exist? On Sunday 02 June 2002 06:54 pm, Philip Mak wrote: > I saw a bunch of Ruby examples on the web that involve the assert() > method. However, I can't seem to use it. Does this method exist / > what do I have to do to be able to access it? > > irb(main):001:0> assert(true) > NameError: undefined method `assert' for # > from (irb):1 The Test::Unit library defines a number of assert methods, but they're only used in test suites, not in regular code. However, it's not hard to write one. This one is a no-op unless the $DEBUG flag is set, which is set by the "-d" command line switch to ruby. ----------- assert.rb ----------- class AssertionFailure < StandardError end class Object def assert(bool, message = 'assertion failure') if $DEBUG raise AssertionFailure.new(message) unless bool end end end --------- assertTest.rb ----------- require 'assert.rb' assert(true, "testing true") assert(false, "testing false") -------------------------------------- $ ruby assertTest.rb $ ruby -d assertTest.rb Exception `AssertionFailure' at ./assert.rb:7 - testing false ./assert.rb:7:in `assert': testing false (AssertionFailure) from assertTest.rb:4 -- Ned Konz http://bike-nomad.com GPG key ID: BEEA7EFE