# from http://redhanded.hobix.com/inspect/stickItInYourIrbrcMethodfinder.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 class MethodFinder def initialize( obj, *args ) @obj = obj @args = args end def ==( val ) MethodFinder.show( @obj, val, *@args ) end end class Object def what?(*a) MethodFinder.new(self, *a) end end "hello".what? == 5 "foo".what?("bar") == "foobar"