How to get the name of the calling alias method?

风流意气都作罢 提交于 2019-12-05 10:34:56

Using __callee__ instead of __method__ will get the name of the alias being run.

I would recommend that, instead of relying on method names and branching (which will become brittle with any method name refactoring, is obviously more complex, etc.) that you simply break apart the methods more to abstract common logic into a shared method.

Here's how I would write it:

def simple_link_to(name, url, options = {})
  # shared_link_to(name) ... or whatever
  link_to(name, url, options)
end

def link_to_admin(name, url, options = {})
  # shared_link_to(name) ... or whatever
  content_tag(:li) do
    simple_link_to(name, url, options)
  end
end

private
def shared_link_to(name)
  # do whatever is common
end

Note: I've made a bit of a guess that you have common code that you'd want to abstract out into some method (which I've called shared_link_to)... but do what you need with that part.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!