Handshake
Handshake is an informal design-by-contract system written in pure Ruby. It’s intended to allow Ruby developers to apply simple, clear constraints to their methods and classes. Handshake is written by Brian Guthrie ([email protected]) and lives at handshake.rubyforge.org.
Here’s an example of Handshake in action:
class NonEmptyArray < Array
include Handshake
invariant { not empty? }
end
class NonEmptyStringArray < NonEmptyArray
contract :initialize, [[ String ]] => anything
contract :<<, String => self
contract :+, many?(String) => self
end
Handshake can also define pre- and post-conditions on your methods.
class Foo
before do
assert( not @widget.nil? )
end
def something_that_requires_widget
...
end
end
See Handshake::ClassMethods for more documentation on exact syntax and capabilities.