Class: Quarry::Probe

Inherits:
Object show all
Defined in:
lib/quarry/probe.rb

Overview

Method Probe

Method::Probe (aka DuckHunter) is a decoy object which 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 it 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 )"]]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProbe

Returns a new instance of Probe.



57
58
59
# File 'lib/quarry/probe.rb', line 57

def initialize
  @ducks, @decoys = {}, {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(aSym, *args) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/quarry/probe.rb', line 65

def method_missing(aSym, *args)
  aSymStr = aSym.to_s

  # This will happen the first time
  @ducks[aSymStr] ||= [] #unless @ducks[aSymStr]
  @ducks[aSymStr] << args.collect { |a| "#{a.class}" }

  decoy = self.dup

  @decoys[aSymStr] ||= [] #unless @decoys[aSymStr]
  @decoys[aSymStr] << decoy

  # build proxy?
  #begin
  #  d = <<-HERE
  #    def self.#{aSymStr}(*args)
  #      # This will happen the subsequent times
  #      @ducks["#{aSymStr}"] << args.collect { |a| #{'"#{a.class}"'} }
  #      @ducks["#{aSymStr}"].uniq!
  #      decoy = self.dup
  #      @decoys["#{aSymStr}"] = [] unless @decoys["#{aSymStr}"]
  #      @decoys["#{aSymStr}"] << decoy
  #      decoy
  #    end
  #  HERE
  #  instance_eval d
  #rescue SyntaxError
  #  puts "This error may be avoidable by returning the failing duck type as the error message."
  #  raise
  #end

  decoy
end

Instance Attribute Details

#decoysObject (readonly)

Returns the value of attribute decoys.



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

def decoys
  @decoys
end

#ducksObject (readonly)

Returns the value of attribute ducks.



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

def ducks
  @ducks
end

Class Method Details

.duckcallObject



46
47
48
49
50
51
52
53
# File 'lib/quarry/probe.rb', line 46

def self.duckcall
  begin
    yield
  rescue TypeError => e
    self.send(e.message)
    retry
  end
end

Instance Method Details

#initialize_copy(from) ⇒ Object



61
62
63
# File 'lib/quarry/probe.rb', line 61

def initialize_copy(from)
  initialize
end