def assert raise "Assertion failed !" unless yield if $DEBUG end
Usage :
def aFunc(i) assert { i < 10 } # ... end $DEBUG = true # Ok. aFunc(5) # Raise Assert Exception. aFunc(15) $DEBUG = false # Ok. aFunc(5) aFunc(15)
12374 users tagging and storing useful source code snippets
Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
def assert raise "Assertion failed !" unless yield if $DEBUG end
def aFunc(i) assert { i < 10 } # ... end $DEBUG = true # Ok. aFunc(5) # Raise Assert Exception. aFunc(15) $DEBUG = false # Ok. aFunc(5) aFunc(15)
You need to create an account or log in to post comments to this site.
However, I'm wondering what the overhead is of having the block at all; that is, how much work has to be done to generate the context the caller passes to the method. Depending on the implementation, that might be considerably more than was saved, at least if the assertion test is something fast like "i<10". For an expensive test your approach would always be a win, though.