module MethodFinder
Constants
- VERSION
Public Class Methods
debug?()
click to toggle source
Checks whether or not debugging is currently enabled
# File lib/methodfinder.rb, line 61 def self.debug? @debug end
find(obj, res, *args, &block)
click to toggle source
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#[]", ...] MethodFinder.find("abc","ABC") #=> ["String#swapcase", "String#swapcase!", "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", ...]
# File lib/methodfinder.rb, line 83 def self.find(obj, res, *args, &block) find_methods(obj) do |met| o = obj.dup rescue obj m = o.method(met) next unless m.arity <= args.size STDERR.puts(met) if debug? a = args.empty? && ARGS.key?(met) ? ARGS[met] : args m.call(*a, &block) == res rescue nil end end
find_classes_and_modules()
click to toggle source
Returns all currently defined modules and classes.
# File lib/methodfinder.rb, line 95 def self.find_classes_and_modules with_redirected_streams do constants = Object.constants.sort.map { |c| Object.const_get(c) } constants.select { |c| c.class == Class || c.class == Module } end end
find_in_class_or_module(klass, pattern = /./)
click to toggle source
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]
# File lib/methodfinder.rb, line 118 def self.find_in_class_or_module(klass, pattern = /./) klasses = Object.const_get(klass.to_s) class_methods = klasses.methods(false) rescue [] instance_methods = klasses.instance_methods(false) all_methods = class_methods + instance_methods all_methods.grep(/#{pattern}/).sort end
toggle_debug!()
click to toggle source
Toggles the debug mode
# File lib/methodfinder.rb, line 66 def self.toggle_debug! @debug = !@debug end
Private Class Methods
methods_to_try(obj)
click to toggle source
Returns a list of candidate methods for a given object. Added by Jan Lelis.
# File lib/methodfinder.rb, line 127 def self.methods_to_try(obj) ret = obj.methods ignorelist = select_ignorelist(obj) klass = obj.is_a?(Module) ? obj : obj.class klass.ancestors.each { |ancestor| ret -= ignorelist[ancestor.to_s.intern] } ret.sort end