4

Similar to __callee__, is there something which returns the calling method? I realize there is the caller which I amble to strip the name of the caller method from but I am curious is there is a standard method for returning the name of the calling method without any other information along with it.

rudolph9
  • 8,021
  • 9
  • 50
  • 80

1 Answers1

2

There is no such feature in MRI. But there are some alternatives.

In case you happen to use Rubinius, you can do this instead of parsing caller:

Rubinius::VM.backtrace(1, false).first.name
#=> :calling_method_name

You can also use a gem to parse the result of caller for you. It should work for any Ruby > 1.9.

The answer to this SO question describes how you can do some simple parsing yourself.

And finally, there appears to be work in progress on getting a feature like this into Ruby 2.0, although the relevant ticket has not been updated for a while.

Community
  • 1
  • 1
molf
  • 73,644
  • 13
  • 135
  • 118