defined icon indicating copy to clipboard operation
defined copied to clipboard

Defined breaks ruby debugger

Open Xzyx987X opened this issue 14 years ago • 0 comments

As the title states, when using the defined gem, ruby's debugger will not work correctly. This seems to be due to a poor design decision that makes it so only one trace function can be defined at a time. Here is a fix:

SET_TRACE_FUNC = method(:set_trace_func).to_proc

module Kernel

@@tracer = nil

def set_trace_func(proc) @@tracer = proc return SET_TRACE_FUNC.call(proc) end

def get_trace_func() return @@tracer end

end

somewhat ungly hack to make the debugger work should it already be included at this point

if defined?(DEBUGGER__) set_trace_func proc { |event, file, line, id, binding, klass, *rest| DEBUGGER__.context.trace_func event, file, line, id, binding, klass } end

module Defined class << self

#*snip*

def disable!
  set_trace_func @old_trace
  @enabled = false
end

def enable!
  @old_trace = get_trace_func
  set_trace_func method(:trace_function).to_proc
  @enabled = true
end

#*snip*

def trace_function(event, file, line, method, binding, klass)
  @old_trace.call(event, file, line, method, binding, klass) if @old_trace != nil
  if definition_start?(event, method, klass)
    definitions << eval('self', binding)
  elsif definition_end?(event, method, klass)
    object = definitions.pop
    method ||= object.__class__.name.downcase.to_sym
    object.defined(file, line, method) if object.respond_to?(:defined)
  end
end

end end

Maybe you could come up with something better, but this seems to work well enough.

Xzyx987X avatar Nov 29 '11 02:11 Xzyx987X