This is a follow-up question to How to determine the class a method was defined in? (hope you don't mind the similarity)
Given a class hierarchy, can a method retrieve its own Method
instance?
class A
def foo
puts "A#foo: `I am #{method(__method__)}'"
end
end
class B < A
def foo
puts "B#foo: `I am #{method(__method__)}'"
super
end
end
A.new.foo
# A#foo: `I am #<Method: A#foo>'
B.new.foo
# B#foo: `I am #<Method: B#foo>'
# A#foo: `I am #<Method: B#foo>' # <- A#foo retrieves B#foo
So that B.new.foo
instead prints
# B#foo: `I am #<Method: B#foo>'
# A#foo: `I am #<Method: A#foo>' # <- this is what I want
In the previous question, Jörg W Mittag suspected that retrieving the class a method was defined in might violate object-oriented paradigms. Does this apply here, too?
Shouldn't a method "know itself"?
I found a method that exactly does that.
class A
def foo
puts "A#foo: `I am #{method(__method__).super_method || method(__method__)}'"
end
end
I really admire Matz and the Ruby core developers. The existence of such method means that they had in mind such situation, and had thought about what to do with it.
Building on answer of How to determine the class a method was defined in? and @sawa's answer with respect to super_method
, you can use:
def meth(m, clazz)
while (m && m.owner != clazz) do
m = m.super_method
end
return m
end
class A
def foo
puts "A#foo: `I am #{meth(method(__method__), Module.nesting.first)}'"
end
end
class B < A
def foo
puts "B#foo: `I am #{meth(method(__method__), Module.nesting.first)}'"
super
end
end
B.new.foo
# B#foo: `I am #<Method: B#foo>'
# A#foo: `I am #<Method: A#foo>'
Idea here is that since we know the class/module where the method is defined by Module.nesting.first
, we take the current method object found by method(__method__)
and iterate through its super
chain to find that instance of method whose owner is same as the class/module that defined the method.