1

Possible Duplicate:
How to get the name of the calling method?

I have a method #foo. That method can be called from many places (scopes). I need #foo to find out where it was called from. But not the stack trace (like what #caller returns), I want the pure name of the method (like what #__callee__ returns). Does a method for this exist in Ruby?

Community
  • 1
  • 1
Janko
  • 8,985
  • 7
  • 34
  • 51
  • 1
    Not a built-in method, but there's a similar question here: http://stackoverflow.com/questions/5100299/how-to-get-the-name-of-the-calling-method-in-ruby – Pete Schlette Aug 25 '12 at 22:28
  • Yeah, I guess it's not so bad to use it like that. Thanks. – Janko Aug 25 '12 at 22:48

1 Answers1

0

you can use __method__. Place that in any method body and it should provide the name of the method. And you can always fetch the context by using .class or .class.ancestors.

Shailen Tuli
  • 13,815
  • 5
  • 40
  • 51
  • 1
    Hmm, it seems like `__method__` returns the same as `__callee__` (the name of the current scope). I want the previous scope. So, if `#bar` called `#foo`, I want a method that I can call from `#foo` which would return `"bar"` – Janko Aug 25 '12 at 22:08