Subclassing Binding

  • Thread starter trans. (T. Onoma)
  • Start date
T

trans. (T. Onoma)

How can I subclass the Binding class?

class TracePoint < Binding
def initialize(binding)
# what here?
# self.replace(binding) ?
end
end
b = binding()
tp = TracePoint.new(b)

Must I delegate, instead?

T.
 
T

trans. (T. Onoma)

What do you hope to accomplish by doing this?

Short answer:

class TracePoint #< Binding

def initialize(event, binding, back_binding=nil)
@event = event
@binding = binding # delegate!!!
@back_binding = (back_binding ? back_binding : binding)
end

# ...

def self.trace(&yld)
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,b,bb)
yld.call(tp)
}
end

end


T.
 
P

Paul Brannan

Short answer:

class TracePoint #< Binding

def initialize(event, binding, back_binding=nil)
@event = event
@binding = binding # delegate!!!
@back_binding = (back_binding ? back_binding : binding)
end

This doesn't really answer my question well. Why do you want to inherit
from binding here instead of delegating to it? Delegation in Ruby is so
easy that it's often preferred over inheritance.

Paul
 
T

trans. (T. Onoma)

This doesn't really answer my question well. Why do you want to inherit
from binding here instead of delegating to it? Delegation in Ruby is so
easy that it's often preferred over inheritance.

Okay, well, delegation works okay. At least I don't see any significant
drawbacks --albeit there may be some subtle distinctions I am not aware-of.
So really, the point is simply that a TracePoint is "formally" supposed to be
a Binding. That's all.

BTW, robert pointed out that this improvement over the above:

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

Also, I should point out that the back_binding exists because a set_trace_func
'return' event gives a binding outside to the returning method, rather then
inside of it. I guess this is how it's supposed to be, but for many purposes
the inner binding is still needed. It would be nice if set_trac_func dealt
with this. Actually what I will eventually propose is that set_trac_func use
TracePoint instead, as well as a Kernel#call_stack method. --one of the
important things I realized was that a Binding actually carries all the other
info trace_set_fuc returns, except for the event type.

T.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,416
Latest member
LionelQ387

Latest Threads

Top