#!/usr/bin/env ruby -w $STUB = true module Stub REVISION = '$Id: stub.rb,v 1.3 2003/02/17 05:42:21 drbrain Exp $' end class Object def method_missing(meth, *args) result = nil method_exists = false my_caller = caller[1] || caller[0] fn = File.basename(my_caller.split(':').first, '.rb') + '_missing.rb' begin unless my_caller =~ /in `method_missing'/ then load fn result = self.__send__(meth, *args) method_exists = true end rescue LoadError => err rescue RuntimeError => err # XXX may not be needed if err.message =~ /not implemented \(stub\)/ then method_exists = true end end unless method_exists then File.open(fn, 'a') do |fp| fp << "class #{self.class}\n" fp << " def #{meth}" if args.length > 0 then fp << "(" fp << (1..args.size).map { |x| "arg#{x}" }.join(", ") fp << ")" end fp << "\n" fp << " unless $STUB then\n" fp << " raise RuntimeError, '#{self.class}##{meth} not implemented (stub)'\n" fp << " end\n end\nend\n" end end return result end end if __FILE__ == $0 then def baz foo end foo true.bar baz end