ITeye博客

令人迷惑的class_eval和instance_eval

googya · 2010-05-31 17:27 · JRuby&Ruby · 编程语言 · Markdown

按照yugui 大姐的说法,class_eval和instance_eval有如下的区别:

class&instance_eval              self                            default definee
class_eval                          the receiver                   the receiver
instance_eval                   the receiver                     eigenclass of the receiver

上面就是class_eval与instance_eval的区别了,一般情况下这个没有问题(个人感觉)。但还是会有令迷惑的地方。
下面的例子说明了,在下面情况下,class_eval和instance_eval的作用没有区别。

class A
end
class Object
	def eigenclass
		class << self;self;end
	end
end
A.class_eval do
p self
	def hehe
		"hehe"
	end
	define_method(:xixi) do
		"xixi"
	end
end

A.instance_eval do
p self
	def haha;"haha";p self ;end
end

A.instance_eval do
p self
	define_method(:hihi) do
		"hihi"
		p self.to_s
	end
end
 
 A.haha
 A.new.hihi
 p A.instance_methods
 p  "*"*120
 p A.eigenclass.instance_methods

    定义的hihi和xixi都是A是实例方法。

 jayfield  
说到:

define_method always defines an instance method on the target (Foo in our example). This makes sense because define_method is being call on the implicit self, which evaluates to the receiver (Foo) in our example."[/

 

jayfield的说法应该是有问题的,但不这样理解,上面的例子怎么又说得通呢?

PS:可能是我表述不清还是怎么的,基本上没有人对此关注。实际上我想说的就是:

A.instance_eval do 
p self 
def haha;"haha";p self ;end 
end 

A.instance_eval do 
p self 
define_method(:hihi) do 
"hihi" 
p self.to_s 
end 
end 

 

   同样是用instance_eval,为什么有这么大的不同。

A.new.hihi 
A.haha 

 

事实上,

A.instance_eval do
p self
define_method(:hihi) do
"hihi"
p self.to_s
end
end

 

 

与 class A

 

  define_method(:hihi) do

     "hihi"

 

end

end

的效果是一样的,define_method是个私有方法,能调用(隐式)它的只能是类对象而非一般的对象。

 

 

 

 

  在一个网站 上看到了这个:

 

 

现在可以比较直观的理解这二者的不同了

评论(16)

  1. #16 楼 · googya · 2011-10-04 16:01

    googya writes:

    [quote=axgle]define_method always defines an instance method on the target[/quote] A.instance_eval do
    p self
    define_method(:hihi) do
    "hihi"
    p self.to_s
    end
    end
    axgle说的是一般情况。上面这个例子就是定义了一个实例方法,如何解释?

    给类对象创建一个实例方法。。。。。那个target就是 A,不管A用instance_eval 或者是 class_eval都是一样的

  2. #15 楼 · consy · 2010-12-29 10:56
  3. #14 楼 · lllyq · 2010-09-27 20:25
  4. #13 楼 · googya · 2010-09-27 12:39

    从这么多个给我投新手帖的情况来看,我觉得可以反映出2个问题:

    1、很多人很轻视这个问题。yehuda这个Ruby顶尖高手都曾经在这个问题上犯过错误, 可 见这个问题确实有点迷惑性,因而我觉得有讨论它的价值。从回帖的情况来看,并不是所有人都认清了这个问题的本质。

    2、不宽容。那些没有留下只言片语却投了新手帖的人,我不知道他们是什么心态。难道他们都是nb的不行的人物?也许吧,那请你回答这个问题啊,或者可以给点提示、努力的方向都可以啊。

    当然对我而言,确实是Ruby新手。但是这个问题却不是那么的simple,也不是那么的stupid。

  5. #12 楼 · googya · 2010-09-27 12:27

    lllyq writes:

    顾名思义 instance_eval 就是实例级别的eval class_eval 就是类级别的eval

    A.instance_eval => open A's singleton instance A.new.instance_eval => open A.new A.class_eval => open A

    嗯,你说的不错,确实,按照字面上的理解确实如此! 但是关键的问题在于:

    
    A.instance_eval do
    
        define_method(:hihi) do  
            "hihi"  
            p self.to_s  
       end  
    end
    
    

    这二者的组合产生的效果是什么样的。 A.instance_eval 打开了通往A的eigenclass 的门,在那个门内的方法,按道理来说,仅仅是为A定义的,也就说应该是A单独享有的方法(其实例都不能享有)。但是实际的情况并非如此!,hihi这个方法是为A的实例定义的,这也是我疑惑的地方所在。

    不过我想我已经弄清楚了具体的原因!
    
  6. #11 楼 · ye_jian_hui · 2010-09-25 15:08

    lllyq writes:

    顾名思义 instance_eval 就是实例级别的eval class_eval 就是类级别的eval

    A.instance_eval => open A's singleton instance A.new.instance_eval => open A.new A.class_eval => open A

    正解!

  7. #10 楼 · lllyq · 2010-09-24 17:01

    顾名思义 instance_eval 就是实例级别的eval class_eval 就是类级别的eval

    A.instance_eval => open A's singleton instance A.new.instance_eval => open A.new A.class_eval => open A

  8. #9 楼 · tjcjc · 2010-08-31 21:23

    target就是self吧 A.instance_eval{puts self} A.class_eval{puts self} 出来的结果都是A 所以在这两个里面的define_method都是给A定义一个实例方法 instance_eval与class_eval只是给define_method提供了一个self环境

  9. #8 楼 · googya · 2010-08-31 11:00

    axgle writes:

    define_method always defines an instance method on the target

    关键问题是:这个target是指的什么

  10. #7 楼 · xds2000 · 2010-06-06 22:05

    我也困惑过。我也来谈谈。 instance_eval,名字上就有instance,就是实例。 什么是实例?a = A.new ,这个a就是实例。 我再自问一句,a实例是什么? 因为在ruby世界里什么都是对象,那个a这个对象其实就是A的单例。 所以你可以这样 def a.method1 puts 'this is singleton method' end

    a.method1

    所以也会有 class A end

    a = A.new a.instance_eval do self # => a

    current class => a's singleton class

    def method1 puts 'this is a singleton method of instance a' end end

    a.method1 #=> this is a singleton method of instance a

    b = A.new b.method1 #=>NoMethodError: undefined method `method1' for #<A:0x10043ff70>

    我在想,Ruby是一个语言的大杂烩,不会像Python那样优雅,所以它一定是遵循一种简单的语义。所以我们不能像别其它语言那样,把高度看太高,其实想过来很简单。

    coding in fun!

    class_eval是为Class定义函数。 我知道只要在Class里定义的函数,它的实例就相应得到了这些函数。 class A end

    a = A.new a.method1 #=> NoMethodError: undefined method `method1' for #<A:0x10043ff70>

    A.class_eval do self # => A

    current class => A

    def method1 puts 'this is a instance method of class A' end end

    a.method1 #=> this is a instance method of class A

    参考: http://note.jiaeil.com/2010/05/16/digging-ruby-instance_eval-and-class_eval/

  11. #6 楼 · googya · 2010-06-06 08:15
  12. #5 楼 · hf_cn81 · 2010-06-05 11:10
  13. #4 楼 · googya · 2010-06-04 13:12
  14. #3 楼 · hf_cn81 · 2010-06-04 09:26
  15. #2 楼 · googya · 2010-06-01 09:12

    axgle writes:

    define_method always defines an instance method on the target

    A.instance_eval do
    p self
    define_method(:hihi) do
    "hihi"
    p self.to_s
    end
    end
    axgle说的是一般情况。上面这个例子就是定义了一个实例方法,如何解释?

  16. #1 楼 · axgle · 2010-05-31 20:00

    define_method always defines an instance method on the target

评论功能维护中,当前仅展示历史评论。