Description

This project was inspired by Smalltalk's Method Finder.

Provided with a receiver, a desired result and possibly some arguments, MethodFinder.find will list all methods that produce the given result when called on the receiver with the provided arguments.

This gem also adds Object#find_method, which besides offering an alternate interface to pretty much the same functionality, also allows you to test for state other than the return value of the method.

All of this probably sounds more complicated than it really is, just look at the examples below.

Usage

>> MethodFinder.find(10,1,3)
=> [:%, :<=>, :>>, :[], :modulo, :remainder]
>> MethodFinder.find("abc","ABC")
=> [:swapcase, :swapcase!, :upcase, :upcase!]
>> MethodFinder.find(10,100,2)
=> [:**]
>> MethodFinder.find(['a','b','c'],['A','B','C']) { |x| x.upcase }
=> [:collect, :collect!, :map, :map!]

Thanks to a suggestion by Ryan Bates, this gem now also provides an alternative interface:

>> %w[a b c].find_method { |a| a.unknown(1) ; a == %w[a c] }
=> [:delete_at, :slice!]
>> 10.find_method { |n| n.unknown(3) == 1 }
=> [:%, :<=>, :>>, :[], :gcd, :modulo, :remainder]

Inside find_method's block, the receiver is available as block argument and the special method unknown is used as a placeholder for the desired method.

Warning

Common sense not included!

While I never had any problems with this, it's still better to be safe than sorry, so use this with caution and maybe not on production data.

I initially wrote this for the students of the core Ruby course on RubyLearning, so Rails is not of interest to me (not saying it doesn't work there, just that I test in plain IRB, not with script/console).

Todo

  • a method black list
  • maybe an alternate form of calling this (issue #3)

Thanks

  • Matthew Lucas for packaging it as a gem.

License

Copyright (c) 2011 Michael Kohl

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.