protocol – Method Protocols for Ruby Classes

Description

This library offers an implementation of protocols against which you can check the conformity of your classes or instances of your classes. They are a bit like Java Interfaces, but as mixin modules they can also contain already implemented methods. Additionally you can define preconditions/postconditions for methods specified in a protocol.

Here's a small example of how a Stack protocol might be specified:

StackProtocol = Protocol do
  def push(x)
    postcondition { top == x }
    postcondition { result == myself }
  end
 
  def top() end
 
  def size() end
 
  def empty?()
    postcondition { size == 0 ? result : !result }
  end
 
  def pop()
    s = size
    precondition { not empty? }
    postcondition { size == s - 1 }
  end
end

Then conformity to the protocol can be declared and checked like this (preconditions and postconditions will be checked during runtime):

class S
  ...
  conform_to StackProtocol
end

More involved examples can be foud in the examples subdirectory of the source distribution.

Installation

The library can be installed via rubygems:

# gem install protocol

The gem and the source archive can also be downloaded directly from rubyforge.org.

If you want to install this library from the source archive you also need Ryan Davis' ParseTree extension.

Author

Florian Frank <flori@ping.de>

License

This is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License Version 2 as published by the Free Software Foundation: GNU GPL.

Valid XHTML 1.0 Transitional