Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

A Smalltalk like Method Finder for Ruby

branch: master
Octocat-spinner-32 lib Update rdoc November 06, 2013
Octocat-spinner-32 test Remove support for Ruby 1.8.7 and apply minor style changes October 30, 2013
Octocat-spinner-32 .gitignore Add doc/ to .gitignore November 06, 2013
Octocat-spinner-32 .travis.yml More CI November 06, 2013
Octocat-spinner-32 Gemfile More CI November 06, 2013
Octocat-spinner-32 Gemfile.lock More CI November 06, 2013
Octocat-spinner-32 LICENSE Changed year February 10, 2011
Octocat-spinner-32 README.rdoc More CI November 06, 2013
Octocat-spinner-32 Rakefile Add rdoc tasks November 06, 2013
Octocat-spinner-32 methodfinder.gemspec Update gemspec November 06, 2013
README.rdoc

MethodFinder

This project was originally inspired by Smalltalk's Method Finder, but additonal features were added over time.

Requirements

Ruby 1.9.3+ (also works with Rubinius in 1.9 mode). Versions of MethodFinder up to 1.2.5 will also work with Ruby 1.8.7.

Usage

MethodFinder.find

Provided with a receiver, the 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.

MethodFinder.find(10, 1, 3)
#=> ["Fixnum#%", "Fixnum#<=>", "Fixnum#>>", "Fixnum#[]", "Integer#gcd", "Fixnum#modulo", "Numeric#remainder"]
MethodFinder.find("abc","ABC")
#=> ["String#swapcase", "String#swapcase!", "String#upcase", "String#upcase!"]
MethodFinder.find(10, 100, 2)
#=> ["Fixnum#**"]
MethodFinder.find(['a','b','c'], ['A','B','C']) { |x| x.upcase }
#=> ["Array#collect", "Array#collect!", "Enumerable#collect_concat", "Enumerable#flat_map", "Array#map", "Array#map!"]

Object#find_method

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

%w[a b c].find_method { |a| a.unknown(1) ; a == %w[a c] }
#=> ["Array#delete_at", "Array#slice!"]
10.find_method { |n| n.unknown(3) == 1 }
#=> ["Fixnum#%", "Fixnum#<=>", "Fixnum#>>", "Fixnum#[]", "Integer#gcd", "Fixnum#modulo", "Numeric#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.

You can also call find_method without passing a block. This is the same as calling MethodFinder.find.

10.find_method(1,3)
#=> ["Fixnum#%", "Fixnum#<=>", "Fixnum#>>", "Fixnum#[]", "Integer#gcd", "Fixnum#modulo", "Numeric#remainder"]

Blacklists

You can exclude methods from being tried by editing the hashes MethodFinder::INSTANCE_METHOD_BLACKLIST and MethodFinder::CLASS_METHOD_BLACKLIST. Both use the class/module as key and an array of method names as values (note that class, module and method names have to be symbols).

For example, to blacklist the instance method shutdown of Object, you would do

MethodFinder::INSTANCE_METHOD_BLACKLIST[:Object] << :shutdown

This might come in handy when using MethodFinder together with other gems as such as interactive_editor.

MethodFinder.find_classes_and_modules

A simple method to return all currently defined modules and classes.

MethodFinder.find_classes_and_modules
#=> [ArgumentError, Array, BasicObject, Bignum ... ZeroDivisionError]

MethodFinder.find_in_class_or_module

Searches for a given name within a class. The first parameter can either be a class object, a symbol or a string whereas the optional second parameter can be a string or a regular expression:

MethodFinder.find_in_class_or_module('Array', 'shuff')
#=> [:shuffle, :shuffle!]
MethodFinder.find_in_class_or_module(Float, /^to/)
#=> [:to_f, :to_i, :to_int, :to_r, :to_s]

If the second parameter is omitted, all methods of the class or module will be returned.

MethodFinder.find_in_class_or_module(Math)
#=> [:acos, :acosh, :asin ... :tanh]

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 interestto me (not saying it doesn't work there, just that I test in plain IRB/Pry and not with the Rails console.

Thanks

License

Copyright © 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.

Something went wrong with that request. Please try again.