Class: MethodFinder
Constant Summary collapse
- ARGS =
{ :cycle => [1] # prevent cycling forever }
- INSTANCE_METHOD_BLACKLIST =
Blacklisting methods, e.g. { :Object => [:ri, :vim] }
Hash.new { |h, k| h[k] = [] }
- CLASS_METHOD_BLACKLIST =
Hash.new { |h, k| h[k] = [] }
Class Method Summary collapse
- .find(obj, res, *args, &block) ⇒ Object
- .find_classes_and_modules ⇒ Object
- .find_in_class_or_module(c, pattern = /./) ⇒ Object
-
.methods_to_try(obj) ⇒ Object
Added by Jan Lelis.
Class Method Details
.find(obj, res, *args, &block) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/methodfinder.rb', line 35 def find(obj, res, *args, &block) redirect_streams methods_to_try(obj).select do |met| o = obj.dup rescue obj m = o.method(met) if m.arity <= args.size a = args.empty? && ARGS.has_key?(met) ? ARGS[met] : args m.call(*a, &block) == res rescue nil end end ensure restore_streams end |
.find_classes_and_modules ⇒ Object
62 63 64 65 |
# File 'lib/methodfinder.rb', line 62 def find_classes_and_modules constants = Object.constants.sort.map { |c| Object.const_get(c) } constants.select { |c| c.class == Class || c.class == Module} end |
.find_in_class_or_module(c, pattern = /./) ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/methodfinder.rb', line 67 def find_in_class_or_module(c, pattern=/./) cs = Object.const_get(c.to_s) class_methods = cs.methods(false) rescue [] instance_methods = cs.instance_methods(false) all_methods = class_methods + instance_methods all_methods.grep(/#{pattern}/).sort end |
.methods_to_try(obj) ⇒ Object
Added by Jan Lelis
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/methodfinder.rb', line 51 def methods_to_try(obj) ret = obj.methods.map(&:intern) blacklist = obj.is_a?(Module) ? CLASS_METHOD_BLACKLIST : INSTANCE_METHOD_BLACKLIST klass = obj.is_a?(Module) ? obj : obj.class klass.ancestors.each { |ancestor| ret -= blacklist[ancestor.to_s.intern] } # 1.8.7 lacks Symbol#<=> ret.sort_by(&:to_s) end |