Ruby Weekly is a weekly newsletter covering the latest Ruby and Rails news.

Handshake: Design-by-Contract

By Hendy Irawan / May 22, 2007

Handshake, currently in version 0.3.0, is an informal design-by-contract system written by Brian Guthrie in pure Ruby. It's intended to allow Ruby developers to apply simple, clear constraints to their methods and classes.

Handshake is definitely welcome alongside the ever-increasing number of test-driven and behavior-driven design/development based tools, such as RSpec and test/spec.

Some examples of Handshake contracts (from its documentation):

# An array that can never be empty.
class NonEmptyArray < Array
include Handshake    invariant { not empty? }
end

# An array to which only strings may be added.
class NonEmptyStringArray < NonEmptyArray
contract :initialize, [[ String ]] => anything
contract :<<, String => self
contract :+, many?(String) => self
contract :each, Block(String => anything) => self
end

Visit Handshake project page and Rdoc documentation for more information.

About Hendy Irawan

Ruby programmer from Indonesia. View all posts by Hendy Irawan →

Other Posts to Enjoy

Twitter Mentions