加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Ruby从Class获取实例方法定义

发布时间:2020-12-16 23:31:21 所属栏目:百科 来源:网络整理
导读:我试图从Class对象获得方法的定义:foo. class Bar def foo(required_name,optional="something") puts "Hello args are #{required_name},#{optional}" end def self.bar puts "I am easy,since I am static" endend 我无法创建类的实例,因为我需要方法定义
我试图从Class对象获得方法的定义:foo.
class Bar
  def foo(required_name,optional="something")
    puts "Hello args are #{required_name},#{optional}"
  end

  def self.bar
    puts "I am easy,since I am static"
  end
end

我无法创建类的实例,因为我需要方法定义来评估我应该创建对象(应用程序需求). Bar.class.???(:foo)

我可以使用Bar.class.method(:bar)获得条形定义但当然我需要foo,谢谢!

更新:

使用Ruby 1.8.7

解决方法

您可以在类这样的类上使用方法instance_method:
Bar.instance_method(:foo)

这将返回UnboundMethod的一个实例. (见http://ruby-doc.org/core-1.9.3/UnboundMethod.html)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读