MMSequeira
MMSequeira

Reputation: 61

How to obtain the class binding of a method?

Take

class A; def foo; end; end
class B < A; end
b = B.new          # => #<B:0x0000000243b8c0>
m = b.method :foo  # => #<Method: B(A)#foo>
m.owner            # => A
m.receiver         # => #<B:0x0000000243b8c0>
m.receiver.class   # => B
mm = m.unbind      # => #<UnboundMethod: B(A)#foo>

How can I get B from mm short of parsing the result of mm.to_s?

Upvotes: 0

Views: 50

Answers (1)

Stefan Kanev
Stefan Kanev

Reputation: 3040

I spent some time looking at proc.c and I don't think it is possible. B gets stored in rclass and as far as I can tell it is not accessible anywhere. You can see how it is used here, although I don't think that helps either.

May I ask why do you need to do that? Maybe there is another way to solve the general problem :)

Upvotes: 1

Related Questions