How to return a method name of a call?

Now my code works as such:

def method_a
  self.method_b ==> 'method_b'
end

def method_b
  puts self.name_of_calling_method
end

def name_of_calling_method
  if  /`(.*)'/.match(caller.first)
    return $1
  else
    return nil
  end
end

Instead of the method_b method for printing 'method_b', how can I print the name of the calling method - 'method_a'?

+3
source share
2 answers

Replace caller.firstwith caller[1].

+2
source

When you are in name_of_calling_methodcalled from method_b, then method_athis is 1 record above the call stack, so you want caller[1]to name_of_calling_method, not caller.firstor caller[0].

caller , nil , method_b caller[1] nil - else .

+3

Source: https://habr.com/ru/post/1740208/


All Articles