[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Subclassing Binding

T. Onoma

10/1/2004 3:45:00 AM

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.


4 Answers

Paul Brannan

10/4/2004 1:13:00 PM

0

On Fri, Oct 01, 2004 at 12:44:47PM +0900, trans. (T. Onoma) wrote:
> How can I subclass the Binding class?

What do you hope to accomplish by doing this?

Paul



T. Onoma

10/4/2004 3:10:00 PM

0

On Monday 04 October 2004 09:13 am, Paul Brannan wrote:
> On Fri, Oct 01, 2004 at 12:44:47PM +0900, trans. (T. Onoma) wrote:
> > How can I subclass the Binding class?
>
> 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.


Paul Brannan

10/5/2004 6:01:00 PM

0

On Tue, Oct 05, 2004 at 12:10:08AM +0900, trans. (T. Onoma) wrote:
> 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. Onoma

10/5/2004 6:18:00 PM

0

On Tuesday 05 October 2004 02:00 pm, Paul Brannan wrote:
> On Tue, Oct 05, 2004 at 12:10:08AM +0900, trans. (T. Onoma) wrote:
> > 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.

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.