From: Robert Dober Date: 2008-04-22T20:56:41+09:00 Subject: Ruby and Contract Programming (was Ruby vs. Ada) Hi list I have had some thoughts about Contract Programming and Ruby, this after the recent suggestion that Ruby might have to evolve into this direction. I am a complete ignorant of CP (also aka Programming by Contract and a different copyrighted term) and that is why I was very defensive on the subject. Not having got any reply I will try to come up with some conclusions based on the little I know, hopefully nobody will take offense and rather take the opportunity to correct me where necessary. My first assumption is that Ruby and other dynamic languages do not shine on large scale projects if ( and mostly only if ) there can be no automated testing, and thus BDD/TDD cannot be applied. Whenever BDD (e.g) is applied I have much difficulties to see how CP could come into the picture. As a matter of fact one can see all the contracts in the testcases, only that we are talking behavior, which is mighty efficient. There are other quite striking parallels between CP and BDD, e.g. never write code unless it makes a failing test pass vs. define the contracts first. The remaining question would be could (and should) Ruby try to occupy other niches (talking in terms of evolution?). For the sake of argument let us say yes, I am however not at all convinced that it really should, than I guess this shall be done without sacrificing any of its dynamic qualities. So the type of contract one could establish would be to check for frozen modules/classes, or frozen traits, which I would prefer of course ;). A simple example of how a contract could look like def my_method arg my_module.instance_methods.all?{ |m| arg.respond_to? m} ... I do not see what could be done more (maybe just checking the arity of the methods?) but all more sophisticated enforcements as the interface of a method just do not bear any meaning in Ruby. My conclusion would be not to go for the throat of strongly typed languages (especially not Ada or OCaml) because it would be an error to try to occupy all niches of the ecosystem in general. In particular I see CP quite inadequate for the evolution of Ruby. Ok I am duck (type-) ing now. Cheers Robert -- http://ruby-smalltalk.blogspot.com/ --- Whereof one cannot speak, thereof one must be silent. Ludwig Wittgenstein