class Object { def require: require_block ensure: ensure_block body: body_block { requirement = Contract Requirement new: require_block ensurance = Contract Requirement new: ensure_block retval = nil requirement if_fullfilled: { retval = body_block call } ensure_block if_fullfilled: { retval } } def require: require_block body: body_block { requirement = Contract Requirement new: require_block requirement if_fullfilled: { body_block call } } def ensure: ensure_block body: body_block { require: ensure_block body: body_block } } class Contract { class RequirementError : RuntimeError class Requirement { def initialize: @block def if_fullfilled: block { @block call: [self] block call } def true: expr { { RequirementError new: "#{expr} not true" . raise! } unless: expr } def false: expr { { RequirementError new: "#{expr} not false" . raise! } if: expr } } } # # usage: # class Fixnum { # def / other { # require: @{ # true: $ other is_a?: Fixnum # true: $ other > 0 # # better: # other class is: Fixnum # other is > 0 # # } body: { # "foo" println # } # } # } # f = 10 / 2 # g = 10 / 0 # requirement error