rudolph9
rudolph9

Reputation: 8119

Calling method Ruby1.9

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.

Upvotes: 4

Views: 132

Answers (1)

molf
molf

Reputation: 74985

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.

Upvotes: 2

Related Questions