Class: WhatMethods::MethodFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/what_methods.rb

Constant Summary collapse

@@blacklist =
%w(daemonize display exec exit! fork sleep system syscall what?)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj, *args) ⇒ MethodFinder

Returns a new instance of MethodFinder.



47
48
49
50
# File 'lib/what_methods.rb', line 47

def initialize( obj, *args )
  @obj = obj
  @args = args
end

Class Method Details

.find(anObject, expectedResult, *args, &block) ⇒ Object

Find all methods on [anObject] which, when called with [args] return [expectedResult]



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/what_methods.rb', line 56

def self.find( anObject, expectedResult, *args, &block )
  stdout, stderr = $stdout, $stderr
  $stdout = $stderr = DummyOut.new
  # change this back to == if you become worried about speed and warnings.
  res = anObject.methods.
        select { |name| anObject.method(name).arity <= args.size }.
        select { |name| not @@blacklist.include? name }.
        select { |name| begin 
                 anObject.clone.method( name ).call( *args, &block ) == expectedResult; 
                 rescue Object; end }
  $stdout, $stderr = stdout, stderr
  res
end

.show(anObject, expectedResult, *args, &block) ⇒ Object

Pretty-prints the results of the previous method



71
72
73
74
75
76
77
# File 'lib/what_methods.rb', line 71

def self.show( anObject, expectedResult, *args, &block)
  find( anObject, expectedResult, *args, &block).each { |name|
    print "#{anObject.inspect}.#{name}" 
    print "(" + args.map { |o| o.inspect }.join(", ") + ")" unless args.empty?
    puts " == #{expectedResult.inspect}" 
  }
end

Instance Method Details

#==(val) ⇒ Object



51
52
53
# File 'lib/what_methods.rb', line 51

def ==( val )
  MethodFinder.show( @obj, val, *@args )
end