0

I have:

def outer_method() called_method() end def called_method() puts "name of outer_method" end 

Is it possible for the called_method to get the name of the outer_method without assigning __method__ in the outer_method and then using it as an argument of called_method?

1 Answer 1

1

Yes. An old school way is to go like this:

def outer_method called_method end def called_method puts caller.first[/(?<=`).+(?=')/] end outer_method # >> outer_method 

A more modern and robust way is to do:

def outer_method called_method end def called_method puts caller_locations.first.label end outer_method # >> outer_method 
Sign up to request clarification or add additional context in comments.

2 Comments

is that thing in primitive way Regex?
If you mean regex, or Regexp, then yes. It passes a regex to the method [].

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.