Ruby 觉得 ruby 元编程里的当前类这个名词不是很科学啊。

lihuazhang · March 23, 2012 · Last by skandhas replied at March 23, 2012 · 3128 hits

第四章,关于当前类和当前对象的讨论里 觉得 ruby 元编程里的当前类这个名词不是很科学啊。 应该是当前类的对象。

比较喜欢吐槽 class_eval 影响的是 instance 而 instance_eval 影响的是 class...

#1 楼 @fleuria 书上写的是 class_eval() 方法会同时修改 self 和当前类。

#1 楼 @fleuria 你这个说的好像不是很准确,刚开始是说了类似一句,但是到后来又有补充

这个没有不妥的地方,你想一下顶层空间。当前对象是 main,当前类是 Object。

玉桂大姐专门写了博文阐述这个问题,可以去看看 :)

#1 楼 @fleuria 这个我之前在另一个帖子里提到过。

我给同事的解释是——

Evaluating on the class level provides access to all instances - hence "instance_eval", and evaluating on the instance level provides access to just that class instance - hence "class_eval".

You need to Sign in before reply, if you don't have an account, please Sign up first.