Class: TracePoint

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

Overview

You can’t subclass Binding, so we delegate (which is better anyway).

Constant Summary collapse

EVENT_MAP =

methods for working with events

{
  :all     => ['call', 'c-call', 'return', 'c-return', 'line', 'class', 'end', 'raise'],
  :before  => ['call', 'c-call'],
  :after   => ['return', 'c-return'],
  :call    => ['call'],
  :return  => ['return'],
  :ccall   => ['c-call'],
  :creturn => ['c-return'],
  :line    => ['line'],
  :class   => ['class'],
  :end     => ['end'],
  :raise   => ['raise']
}
@@active =
false

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event, method, bind, back_binding = bind) ⇒ TracePoint

Until Ruby has a built-in way to get the name of the calling method that information must be passed into the TracePoint.



97
98
99
100
101
102
# File 'lib/mega/tracepoint.rb', line 97

def initialize( event, method, bind, back_binding=bind )
  @event = event
  @method = method
  @binding = bind
  @back_binding = back_binding
end

Instance Attribute Details

#back_bindingObject

– instance ——————-



93
94
95
# File 'lib/mega/tracepoint.rb', line 93

def back_binding
  @back_binding
end

#bindingObject

– instance ——————-



93
94
95
# File 'lib/mega/tracepoint.rb', line 93

def binding
  @binding
end

#eventObject

– instance ——————-



93
94
95
# File 'lib/mega/tracepoint.rb', line 93

def event
  @event
end

Class Method Details

.activeObject



62
# File 'lib/mega/tracepoint.rb', line 62

def active ; @@active ; end

.active=(x) ⇒ Object



64
65
66
67
68
69
# File 'lib/mega/tracepoint.rb', line 64

def active=(x)
  @@active = x ? true : false
  unless @@active
    set_trace_func nil
  end
end

.traceObject

Trace execution using a TracePoint.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/mega/tracepoint.rb', line 72

def trace # :yield:
  if active
    bb_stack = []
    set_trace_func proc{ |e, f, l, m, b, k|
      #(p e, f, l, m, b, k, @@bb_stack; puts "---") if $DEBUG
      if ['call','c-call','class'].include?(e)
        bb_stack << b
      elsif ['return','c-return','end'].include?(e)
        bb = bb_stack.pop
      end
      b = bb if ! b    # this sucks!
      tp = TracePoint.new(e,m,b,bb)
      yield(tp)
    }
  end
end

Instance Method Details

#===(e) ⇒ Object

For use in case conditions



148
149
150
# File 'lib/mega/tracepoint.rb', line 148

def ===(e)
  EVENT_MAP[e].include?(@event)
end

#backObject

shorthand for back_binding



108
# File 'lib/mega/tracepoint.rb', line 108

def back ; @back_binding ; end

#bindObject

shorthand for binding



105
# File 'lib/mega/tracepoint.rb', line 105

def bind ; @binding ; end

#calledObject Also known as: method_name, me

Returns the name of the event’s method. This could delegate to the binding if Ruby had an internal way to retrieve the current method name.



117
# File 'lib/mega/tracepoint.rb', line 117

def called ; @method ; end

#event?Boolean

Is the trace point defined or undefined?

Returns:

  • (Boolean)


144
# File 'lib/mega/tracepoint.rb', line 144

def event? ; !! @event ; end

#event_map(e) ⇒ Object



141
# File 'lib/mega/tracepoint.rb', line 141

def event_map(e) ; EVENT_MAP[e] ; end

#eventless?Boolean

Returns:

  • (Boolean)


145
# File 'lib/mega/tracepoint.rb', line 145

def eventless? ; ! @event ; end

#selfObject

Delegates “self” to the binding which in turn delegates the binding object.



112
# File 'lib/mega/tracepoint.rb', line 112

def self ; @binding.self ; end