[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

what is super?

T. Onoma

10/6/2004 4:03:00 PM

I ask b/c I was wondering if getting the binding of super is any way feasible.

class C
def me
x = 1
end
end

class A < C
def me
b = Binding.of_super
puts b.eval "x"
end
end

A.new.me #=> 1

T.


1 Answer

Paul Brannan

10/6/2004 5:41:00 PM

0

On Thu, Oct 07, 2004 at 01:02:51AM +0900, trans. (T. Onoma) wrote:
> I ask b/c I was wondering if getting the binding of super is any way feasible.

"super" is a method call that calls into the base class.

What you want isn't to get the "binding of super" (which is really
meaningless), but the binding for the block that is created when you
call a method. There isn't any good way to do this, but you can hack it
with set_trace_func:

require 'thread'

def call_and_get_binding(obj, method, *args, &block)
critical = Thread.critical
Thread.critical = true
begin
binding = nil
set_trace_func proc { |*x|
if x[0] == "call" then
set_trace_func nil
binding = x[4]
end
}
result = obj.__send__(method, *args, &block)
return binding, result
ensure
Thread.critical = critical
end
end

def foo
x = 1
end

binding, result = call_and_get_binding(self, :foo)
p eval("x", binding) #=> 1

Now why you'd want to do this is beyond me, but have fun...

Paul