Module: MethodFinder

Defined in:
lib/methodfinder.rb,
lib/methodfinder/version.rb

Constant Summary collapse

ARGS =

Default arguments for methods :nodoc:

{
  cycle: [1] # prevent cycling forever
}.freeze
INSTANCE_METHOD_BLACKLIST =

Blacklisting methods, e.g. { :Object => [:ri, :vim] }

Hash.new { |h, k| h[k] = [] }
CLASS_METHOD_BLACKLIST =

Blacklisting class methods

Hash.new { |h, k| h[k] = [] }
VERSION =
'2.2.1'

Class Method Summary collapse

Class Method Details

.debug?Boolean

Checkes whether or not debugging is currently enabled :doc:

Returns:

  • (Boolean)


55
56
57
# File 'lib/methodfinder.rb', line 55

def self.debug?
  !!(@debug)
end

.find(obj, res, *args, &block) ⇒ Object

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", ...]


77
78
79
80
81
82
83
84
85
86
# File 'lib/methodfinder.rb', line 77

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_modulesObject

Returns all currently defined modules and classes.



89
90
91
92
93
94
# File 'lib/methodfinder.rb', line 89

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 = /./) ⇒ Object

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]

:doc:



112
113
114
115
116
117
118
# File 'lib/methodfinder.rb', line 112

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

.find_unknown(obj, &block) ⇒ Object

Used by Object.find_method :nodoc:



133
134
135
136
137
138
139
140
# File 'lib/methodfinder.rb', line 133

def self.find_unknown(obj, &block)
  find_methods(obj) do |met|
    STDERR.puts(met) if debug?
    obj.class.class_eval("alias :unknown #{met}", __FILE__, __LINE__)
    subject = obj.dup rescue obj # dup doesn't work for immutable types
    block.call(subject) rescue nil
  end
end

.toggle_debug!Object

Toggles the debug mode



60
61
62
# File 'lib/methodfinder.rb', line 60

def self.toggle_debug!
  @debug = !@debug
end