what is super?

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

trans. (T. Onoma)

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.
 
P

Paul Brannan

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
 

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,417
Latest member
DarrenGaun

Latest Threads

Top