如何获取调用别名方法的名称? [英] How to get the name of the calling alias method?

查看:322
本文介绍了如何获取调用别名方法的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为link_to_admin的方法,然后为另一个名为simple_link_to

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

如果我致电link_to_admin,我遇到一个问题,我想将值打印到<li>标记

Here I am facing one problem if I call link_to_admin, I want print the value into <li> tag

例如.

def link_to_admin(name,url,options={})

  # My stuff here
   menu = ""
   menu << "<li> #{link_to(name,url,options)}</li>"
   menu.html_safe
end

如果我呼叫simple_link_to,则不需要<li>标记.因此,当前我正在传递一个像li_requiredoptions,然后在我的方法中检查条件.这很正常,我知道这是正确的方法.

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

但是,在尝试找到诸如simple_methodlink_to_admin之类的调用方法之前,我尝试使用:

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]=~/`(.*?)'/

这无法正常工作.

我在layout中调用simple_methodlink_to_admin方法,因此__method__始终仅返回link_to_admin.

I am calling simple_method or link_to_admin methods in layout so __method__ is always returning link_to_admin only.

但是caller[0]=~/(.*?)'/if am calling from layout means, it's returning layout path and if I am calling from any other helper or class`表示它正在返回当前方法.

But caller[0]=~/(.*?)'/if am calling fromlayoutmeans, it's returning layout path and if I am calling from any otherhelperorclass` 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.

注意:-我创建的alias_method仅用于命名转换.除了添加<li>标记之外,我什么也没做.

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.

推荐答案

使用__callee__而不是__method__将获得正在运行的别名的名称.

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

这篇关于如何获取调用别名方法的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆