Assertions: Difference between revisions

Content deleted Content added
→‎{{header|Ruby}}: Make this work with Ruby 1.9. Also just print the exception message, instead of making own message.
Line 59:
}</lang>
To turn off assertions, simply define the <tt>NDEBUG</tt> macro before where <tt><assert.h></tt> is included.
 
There is no mechanism to add a custom "message" with your assertion, like in other languages. However, there is a "trick" to do this, by simply logical-AND-ing your condition with a string constant message, like in the following. Since a string constant is guaranteed to be non-NULL (and hence evaluated as True), and since AND-ing with True is an identity operation for a boolean, it will not alter the behavior of the assertion, but it will get captured in the debug message that is printed:
<lang c>assert(a == 42 && "Error message");</lang>
 
=={{header|C++}}==