Nathan
Nathan

Reputation: 7855

Is there a way to find out the name of the method that resulted in a call to another method in ruby?

I'm just getting started with meta-programming, and am wondering if there's a way to view method chains similar to class ancestral chains?

For example, imagine I have a method that looks like this:

def method_a
  method_b
end

method_b    
  # ..    
end

If I call method_a like so:

method_a

It should run method_b

Is there a way from within method_b to determine that method_a was responsible for calling it?

Upvotes: 0

Views: 68

Answers (2)

thanikkal
thanikkal

Reputation: 3356

If you are using ruby debugger then where command will spit out the call stack for you current code execution

Upvotes: 0

x1a4
x1a4

Reputation: 19485

Take a look at Kernel#caller, which should give you the information you want.

Upvotes: 3

Related Questions