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_IGNORELIST =
Ignoring methods, e.g. { :Object => [:ri, :vim] }
Hash.new { |h, k| h[k] = [] }
- CLASS_METHOD_IGNORELIST =
Ignoring class methods
Hash.new { |h, k| h[k] = [] }
- VERSION =
'2.2.2'
Class Method Summary collapse
-
.debug? ⇒ Boolean
Checks whether or not debugging is currently enabled :doc:.
-
.find(obj, res, *args, &block) ⇒ Object
Provided with a receiver, the desired result and possibly some arguments,
MethodFinder.findwill list all methods that produce the given result when called on the receiver with the provided arguments. -
.find_classes_and_modules ⇒ Object
Returns all currently defined modules and classes.
-
.find_in_class_or_module(klass, pattern = /./) ⇒ Object
Searches for a given name within a class.
-
.find_unknown(obj, &block) ⇒ Object
Used by Object.find_method :nodoc:.
-
.toggle_debug! ⇒ Object
Toggles the debug mode.
Class Method Details
.debug? ⇒ Boolean
Checks whether or not debugging is currently enabled :doc:
61 62 63 |
# File 'lib/methodfinder.rb', line 61 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", ...]
83 84 85 86 87 88 89 90 91 92 |
# 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 ⇒ Object
Returns all currently defined modules and classes.
95 96 97 98 99 100 |
# 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 = /./) ⇒ 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:
118 119 120 121 122 123 124 |
# 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 |
.find_unknown(obj, &block) ⇒ Object
Used by Object.find_method :nodoc:
139 140 141 142 143 144 145 146 |
# File 'lib/methodfinder.rb', line 139 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
66 67 68 |
# File 'lib/methodfinder.rb', line 66 def self.toggle_debug! @debug = !@debug end |