How to get the name of the calling alias method?
ruby, ruby-2.0, ruby-2.1, ruby-on-rails, ruby-on-rails-4
Solution
Using `__callee__` instead of `__method__` will get the name of the alias being run.
Problem
I am having method called `link_to_admin` then I have aliased another method called `simple_link_to` ``` def link_to_admin(name,url,options={}) # My stuff here link_to(name,url,options) end alias_method :simple_link_to, :link_to_admin ``` Here I am facing one problem if I call `link_to_admin`, I want print the value into `<li>` tag Ex. ``` def link_to_admin(name,url,options={}) # My stuff here menu = "" menu << "<li> #{link_to(name,url,options)}</li>" menu.html_safe end ``` And if I call `simple_link_to` no need of `<li>` tag. So currently I am passing one `options` like `li_required` then am checking condition in my method. This is working perfectly, I know this is correct way to do it. ``` def link_to_admin(name,url,options={}) menu = "" menu << options[:li_required] ? "<li> #{link_to(name,url,options)}</li>" : link_to(name,url,options) menu.html_safe end ``` But,Before I was trying to find the calling method like `simple_method` or `link_to_admin`, so I tried with: ``` 1.__method__ 2.caller[0]=~/`(.*?)'/ ``` This is not working as expected. I am calling `simple_method` or `link_to_admin` methods in `layout` so `__method__` is always returning `link_to_admin` only. But `caller[0]=~/`(.*?)'/`if am calling from`layout`means, it's returning layout path and if I am calling from any other`helper`or`class` means it's returning current method. Any other way is there to check method name. I know this is not good question, could any one please tell me any other way. Note:- I have created alias_method for naming conversion only. Other than adding `<li>` tag I am not doing any thing. Not only here am having this doubt from long back. I know I can define one more method and I can do it. Leave my scenario, generally I wants to know this answer.