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

红宝石级别的疯狂

发布时间:2020-12-16 20:24:08 所属栏目:百科 来源:网络整理
导读:我被卡住了我试图动态定义一个类的方法,我不能把我的头围绕着红宝石元类模型.考虑下列课程: class Example def self.meta; (class self; self; end); end def self.class_instance; self; endendExample.class_instance.class # = ClassExample.meta.class
我被卡住了我试图动态定义一个类的方法,我不能把我的头围绕着红宝石元类模型.考虑下列课程:
class Example

  def self.meta; (class << self; self; end); end

  def self.class_instance; self; end

end

Example.class_instance.class # => Class
Example.meta.class           # => Class

Example.class_instance  == Example      # => true
Example.class_instance  == Example.meta # => false

显然这两种方法都返回一个Class的实例.但这两个例子
是不一样的他们也有不同的祖先:

Example.meta.ancestors            # => [Class,Module,Object,Kernel]
Example.class_instance.ancestors  # => [Example,Kernel]

在元类和类实例之间有什么区别?

我发现,我可以发送:define_method到元类动态定义一个方法,但如果我尝试将其发送到类实例,它将无法工作.至少我可以解决我的问题,但我还是想了解为什么它是这样工作的.

更新2010年3月15日13:40

以下假设是否正确

>如果我有一个调用self.instance_eval并定义一个方法的实例方法,它只会影响该类的特定实例.
>如果我有一个调用self.class.instance_eval(它与调用class_eval相同)的一个实例方法,并定义一个方法,它会影响该特定类的所有实例,从而产生一个新的实例方法.
>如果我有一个类方法调用instance_eval并定义一个方法,它将导致所有实例的一个新的实例方法.
>如果我有一个类方法,在meta / eigen类上调用instance_eval,它将导致一个类方法.

我觉得开始对我有意义如果一个类方法中的self会指向本体类,那么它肯定会限制你的可能性.如果是这样,则不可能从类方法内部定义一个实例方法.那是对的吗?

解决方法

当您使用instance_eval时,动态定义单例方法很简单:
Example.instance_eval{ def square(n); n*n; end }
Example.square(2) #=> 4
# you can pass instance_eval a string as well.
Example.instance_eval "def multiply(x,y); x*y; end" 
Example.multiply(3,9) #=> 27

至于上面的区别,你会混淆2件事情:

你定义的元类是在Ruby社区中叫做singelton类或者eigen类的.那个单例类是你可以添加类(单例)方法的类.

对于您尝试使用class_instance方法定义的类实例,只不过是类本身,以证明它,只需尝试向类Example添加实例方法,并检查您定义的class_instance方法是否返回类示例本身通过检查该方法的存在:

class Example
  def self.meta; (class << self; self; end); end
  def self.class_instance; self; end
  def hey; puts hey; end
end

Example.class_instance.instance_methods(false) #=> ['hey']

无论如何,为了总结它,当你想添加类方法,只需将它们添加到该元类.至于class_instance方法是无用的,只需删除它.

无论如何,我建议你阅读this post来掌握Ruby反思系统的一些概念.

UPDATE

我建议你阅读这个漂亮的帖子:Fun with Ruby’s instance_eval and class_eval,
不幸的是,class_eval和instance_eval是混乱的,因为他们以某种方式反对他们的命名!

Use ClassName.instance_eval to define class methods.

Use ClassName.class_eval to define instance methods.

现在回答你的假设:

If I have an instance method which
calls self.instance_eval and defines a
method,it will only affect the
particular instance of that class.

是:

class Foo
  def assumption1()
    self.instance_eval("def test_assumption_1; puts 'works'; end")
  end
end

f1 = Foo.new
f1.assumption1
f1.methods(false) #=> ["test_assumption_1"]
f2 = Foo.new.methods(false) #=> []

If I have an instance method which
calls self.class.instance_eval (which
would be the same as calling
class_eval) and defines a method it
will affect all instances of that
particular class resulting in a new
instance method.

在该上下文中没有instance_eval将定义类本身上的单例方法(不是实例)

class Foo
  def assumption2()
    self.class.instance_eval("def test_assumption_2; puts 'works'; end")
  end
end

f3 = Foo.new
f3.assumption2
f3.methods(false) #=> []
Foo.singleton_methods(false) #=> ["test_assumption_2"]

为了工作,将上述的class_eval替换为instance_eval.

If I have a class method which calls
instance_eval and defines a method it
will result in a new instance method
for all instances.

不:

class Foo
  instance_eval do
    def assumption3()
      puts 'works'
    end
  end
end

Foo.instance_methods(false) #=> []

Foo.singleton_methods(false) #=> ["assumption_3"]

这将使单例方法,而不是实例方法.为了工作,将上述的class_eval替换为instance_eval.

If I have a class method which calls
instance_eval on the meta/eigen class
and defines a method it will result in
a class method.

好吧,那会做出如此复杂的东西,因为它会添加单例方法到单身阶级,我不认为这将有任何实际的用途.

(编辑:李大同)

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

    推荐文章
      热点阅读