Eric Sven Ristad <ristad / mnemonic.com> writes: > How can I achieve the effect of the assert() macro from C in Ruby? > In particular, > > 1. assert(<expr>) will print something like > > "Assertion failed: <expr>, file <file>, line <line>" > > only when <expr> evaluates to false, where <file> and <line> refer > to the location of the assert() call rather than the location of > the assert() definition > > 2. all asserts are ignored if !$DEBUG, ie., > assert(<expr>) does not even evaluate <expr> when !$DEBUG DEBUG = false # or true def assert if DEBUG result = yield raise "Assertion failed" if !result end end assert { 1 == 1 } assert { 1 == 2 } This defers the evaluation of the code in the block until the method 'assert' can determine if DEBUG is set to true. It doesn't print the expression itself, but it does give you a full backtrace. Also, as it's an exception, you can catch it if you want. Regards Dave