Class: Method

Inherits:
Object show all
Defined in:
lib/mega/methodprobe.rb

Overview

:title: Method Probe

Method::Probe (aka DuckHunter) is a decoy object whic is dropped into methods which records the calls made against it –hence a method probe. Of course, it is not perfect –an inescapable matter it seems for any internal probe. There are a couple of issues related to conditionals. Since the method test for a certain condition against the decoy, how is the decoy to respond? Thus ceratin paths in the code may never get exceuted and thus go unmapped. If Ruby had better conditional reflection (i.e. if ‘if’, ‘case’, ‘unless’, ‘when’, etc. were true methods) then this could be fixed by making the Probe reentrant, mapping out variant true/false/nil replies. The likely insurmountable problem though is the Halting problem. A probe can cause some methods to complete execution. It’s pretty rare, but it can happen and little can be done about it (I think).

Note, the alternative to this kind of probe is a program that examines, rather then executes, the code. This would circumvent the above problems, but run into difficulties with dynamic evals. It would also be more complicated, but might prove a better means in the future.

This script is provided for experimetnal purposes. Please inform the author if you find ways to improve it or put to an interesting use.

Synopsis

require 'methodprobe'

def amethod(x)
  x + 1
end

p method(:amethod).signiture
p method(:amethod).signiture(:class)
p method(:amethod).signiture(:pretty)

produces

[["+"]]
[{"+"=>[["Fixnum"]]}]
[["+( Fixnum )"]]

Authors(s)

  • Thomas Sawyer

Defined Under Namespace

Classes: Probe

Instance Method Summary collapse

Instance Method Details

#signature(detail = nil) ⇒ Object

Outputs signiture information.



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/mega/methodprobe.rb', line 144

def signature(detail=nil)
  ds = []
  case detail
  when :complete, :all, :full
    ds = migration
  when :class, :with_class
    migration.each { |dh| ds << dh.ducks }
  when :pp, :pretty, :prettyprint, :pretty_print
    migration.each do |dh|
      responders = []
      dh.ducks.each do |responder, argss|
        argss.each { |args| responders << "#{responder}( #{args.join(',')} )" }
      end
      ds << responders
    end
  else
    migration.each { |dh| ds << dh.ducks.keys }
  end
  return ds
end