I’ve published my first gem on rubyforge: assit. It does runtime assertions for Ruby.
When I got started with Ruby, I was flabbergasted that there weren’t any runtime assertions at all. I have no idea why – even though there are one gadzillion different unit testing whatnots, a decent debugger and even a profiler. But this basic debugging tool, that has been there since the dawning days of C, is flatly missing; although it’d be even more useful in a language that doesn’t do any static checking.
So I’ve written my own little thing, and put it on for everyone to use. Getting started is easy: Just do
sudo gem install assit
and you’ll be ready to use it.
In the code, it’ll work like this:
require 'assit' # Just some examples, there are more assertions assit(defined?(assit)) # Yes, this is self-referential ;-) x = 2 + 3 assit_equal(5, x, "There seems to be a problem with the addition") foo = Foo.new assit_kind_of(Foo, foo)
When the assertion fails, an action will be taken – at the moment you can either print to stderr, or raise an error. But I’m planning for some more actions, and you can configure which one to use.
It’s also possible to disable assit: All the assertion methods will be replaced with calls to an empty method, so that the runtime overhead is minimal.
I decide to let assit add it’s methods to the basic Object class, so that it will be really easy to use. The drawback is that you cannot use the same method names anywhere in your code. That’s why all the methods start with “assit_” – it’s still similiar to “assert”, but unlikely to be a “real” method name. Also, it doesn’t conflict with the unit testing assertions.
The assertion can be disabled at startup. In this case it will not load the assertion methods, but create aliases to an “empty” method that just returns nil. The only overhead will be the call to the dynamically created method (which should be o.k. by Ruby standards).
If I’ve time, there will be more to come; for example I’d really like an action that automatically breaks to the debugger if an assertion fails.