Andy Hunt and I have been discussing such a language extension.  DBC will
definitely be in my version of Ruby.

Andy coded a cool dbc extension but, like you, I think it merits being part
of the language.

So far:

def MyClass
  def invariant
    ...
  end

  def my_method
    pre { ... }
    post { ... }
    # code here...
  end
end

MyClass#invariant is a special method, like 'initialize'.  It automatically
performs a super call.
'pre' executes at the start of the method, raising an exception if the block
returns false.
'post' executes at the end of the method, also raising an exception if the
block returns false.
'invariant' also gets called at the end of the method, raising an exception
if it returns false.  'invariant' may start a chain of 'invariant' method
calls through the class hierarchy.
'invariant' may also have to be called if a 'super' is within a method.

You may be able to disable/enable dbc for a class by:

MyClass.dbc( true/false )

perhaps some need to disable just 'invariant' calls:

MyClass.dbc_invariant( true/false )

In my version of Ruby, I'll be making sure that there is no performance
penalty if dbc is turned off.  I intend to do this by slightly modifying the
bytecode for methods when you call the dbc enable/disable method.  It's
possible to change the method bytecode start address and insert a return
before post and invariant calls to disable dbc with no loss of performance.

Thing is, I'd hate to implement this and then find out that Matz has
implemented a different scheme for his next version!

--
Justin Johnson