Would this suffice? Its syntax is limited compared to the 'raise'
statement, but if YAGNI...
module Kernel
def do_raise
def maybe_assert(msg,&blk) raise msg unless blk.call end
end
def dont_raise
def maybe_assert(msg) end
end
dont_raise
end
And replace use of 'raise ... unless ... ' with 'maybe_assert ... do ...
end' in the code below.
I don't know what the performance hit of calling an empty method is,
though....
Devin
John Carter wrote:
> I tend to do a mix of Test Driven Development and Design By Contract.
>
> I also tend to write code that chugs away for hours on a fast machine
> even when tightly optimized and all preconditions and invariants
> commented out.
>
> Can anyone think of a convenient way of taking a class like....
>
>
> class MyHairyClass
> def invariant
> raise "Invariant Failure: Bah" unless
> deep_expensive_check_on_the_invariants_of_the_class
> end
>
> def my_public_method( foo)
> invariant
> raise "Precondition failure: Blah" unless
> some_expression_involving_foo
>
> # DO STUFF
>
> raise "Postcondtion failure: Bloo" unless
> some_expression
> invariant
> end
>
> end
>
> and being able to selectively switch on and off the precondition and
> postcondition and pre and post invariant checks.
>
> ie. I want all checks on maximum when I run the unit tests, I want
> them all switched off (compiled out) when I'm running the two hour
> long deep thought, and I want them selectively switched on when it
> crashes half way through the big run. eg. All the precond checks on.
>
> The simplist would be...
>
> if $debug
> def invariant
> # Do check
> end
> else
> def invariant
> end
> end
>
> but that still does two method calls in some of my inner loops and
> doesn't handle the {pre,post}conditions nicely.
>
> John Carter Phone : (64)(3) 358 6639
> Tait Electronics Fax : (64)(3) 359 4632
> PO Box 1645 Christchurch Email : john.carter / tait.co.nz
> New Zealand
>
> Carter's Clarification of Murphy's Law.
>
> "Things only ever go right so that they may go more spectacularly
> wrong later."
>
>> From this principle, all of life and physics may be deduced.
>
>
>