3

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"?

2
  • Perhaps, a cheat will be somehow making that an interned string, and then calling it once from the original location. Commented Jan 12, 2016 at 14:02
  • 1
    @sawa printing the method instance is not the point, although a frozen string could probably work in that case ;-) Commented Jan 12, 2016 at 14:05

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

8 Comments

Ruby 2.2 + only. Not sure output is as expected, never prints B#foo. Also, does not work - if we had class A < C where C is new parent class.
@WandMaker Probably the definition on class A was the main point of Stefan. So I removed the definition for B. If it is needed, Stefan's original code can be used for that part
@WandMaker Regarding your point with C, that is not a problem. We can define the method in two different ways depending on the need. If we want it to return the lexical method name, we can use what I gave in the answer. If we want the method name to depend on the receiver, we can use Stefan's original code.
I was just comparing the output of your method with the expected output in question.
super_method seems to be the way to go, although it feels more like a hack (invoking super_method from A#foo seems illogical because it has no super method; or to put it another way: sometimes the method's body is executed as A#foo and sometimes as B#foo). @WandMaker however is right regarding C: if we had class A < C and class C; def foo; end; end, then A.new.foo would print A#foo: `I am #<Method: C#foo>'
|
2

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.

3 Comments

So you agree that using super_method is on the right track?
@sawa Yea, it surely helps
Traversing the super_method chain up to the current method owner is an interesting approach. It surely fixes the "parent problem" with C.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.