# from http://www.nobugs.org/developer/ruby/method_finder.html class Object # Clone fails on numbers, but they're immutable anyway def megaClone begin self.clone; rescue; self; end end end class MethodFinder # Find all methods on [anObject] which, when called with [args] return [expectedResult] def self.find( anObject, expectedResult, *args ) anObject.methods.select { |name| anObject.method(name).arity == args.size }. select { |name| begin anObject.megaClone.method( name ).call(*args) == expectedResult; rescue; end } end # Pretty-prints the results of the previous method def self.show( anObject, expectedResult, *args ) find( anObject, expectedResult, *args ).each { |name| print "#{anObject.inspect}.#{name}" print "(" + args.map { |o| o.inspect }.join(", ") + ")" unless args.empty? puts " == #{expectedResult.inspect}" } end end # Look for string length method; ie. something which returns 5 when called on "hello" MethodFinder.show( "hello", 5 ) # Look for string concatenation MethodFinder.show( "foo", "foobar", "bar" ) # Look for floor function MethodFinder.show( 3.14159, 3 )