I was studying how the Ruby interpreter is implemented, and one question occurred that didn't get an answer yet for me. That's the one in the title: since Class
(r_cClass
) has super
set to itself (ignoring metaclasses, since actually super
is the metaclass of r_cClass
), if I send one method to the Class
object, this will be looked in the method table of Class
' class. But Class
' class is Class
, so shouldn't I end up looking the instance methods of Class
? But that's not the case since in the documentation Class
class methods and Class
instance methods are separated. In the search_method
in eval.c
of Ruby, I didn't find any special check for the Class
class. Can anyone shed some light on this?
我正在研究如何实现Ruby解释器,并且发生了一个问题,但我还没有得到答案。这是标题中的那个:因为Class(r_cClass)已经超级设置为自身(忽略元类,因为实际上super是r_cClass的元类),如果我向Class对象发送一个方法,这将在方法表中查找班级。但是Class'类是Class,所以我最终不应该查看Class的实例方法?但事实并非如此,因为在文档中Class类方法和Class实例方法是分开的。在Ruby的eval.c中的search_method中,我没有找到Class类的任何特殊检查。任何人都可以对此有所了解吗?
1 个解决方案
#1
3
Your beliefs about the way it should work seem right, but I'm not sure why you think it doesn't work that way.
你对它应该如何工作的看法是正确的,但我不确定为什么你认为它不会那样工作。
In Ruby 1.8.7:
在Ruby 1.8.7中:
irb> a = Class.new.methods - Object.new.methods
=> [... 36 element array ...]
irb> b = Class.methods - Object.new.methods
=> [... 37 element array ...]
irb> b - a
=> ["nesting"]
A normal class instance (Class.new) has 36 instance methods. If I look at Class itself, which is also a normal class instance, it has the same 36 instance methods, plus 1 additional class method (nesting), which exists only because it is inherited from its superclass Module.
普通的类实例(Class.new)有36个实例方法。如果我查看Class本身,它也是一个普通的类实例,它有相同的36个实例方法,加上1个额外的类方法(嵌套),这只是因为它继承自它的超类Module而存在。
Note that adding an instance method to Class automatically adds it as a class method as well, but adding a class to Class's metaclass will not.
请注意,向Class添加实例方法会自动将其添加为类方法,但不会向Class的元类添加类。
irb> class Class ; def everywhere ; true ; end ; end
irb> class << Class ; def only_singleton ; true ; end ; end
irb> Class.everywhere
=> true
irb> Class.new.everywhere
=> true
irb> Class.only_singleton
=> true
irb> Class.new.only_singleton
NoMethodError: undefined method 'only_in_singleton' for #<Class:0x4800ac8>
#1
3
Your beliefs about the way it should work seem right, but I'm not sure why you think it doesn't work that way.
你对它应该如何工作的看法是正确的,但我不确定为什么你认为它不会那样工作。
In Ruby 1.8.7:
在Ruby 1.8.7中:
irb> a = Class.new.methods - Object.new.methods
=> [... 36 element array ...]
irb> b = Class.methods - Object.new.methods
=> [... 37 element array ...]
irb> b - a
=> ["nesting"]
A normal class instance (Class.new) has 36 instance methods. If I look at Class itself, which is also a normal class instance, it has the same 36 instance methods, plus 1 additional class method (nesting), which exists only because it is inherited from its superclass Module.
普通的类实例(Class.new)有36个实例方法。如果我查看Class本身,它也是一个普通的类实例,它有相同的36个实例方法,加上1个额外的类方法(嵌套),这只是因为它继承自它的超类Module而存在。
Note that adding an instance method to Class automatically adds it as a class method as well, but adding a class to Class's metaclass will not.
请注意,向Class添加实例方法会自动将其添加为类方法,但不会向Class的元类添加类。
irb> class Class ; def everywhere ; true ; end ; end
irb> class << Class ; def only_singleton ; true ; end ; end
irb> Class.everywhere
=> true
irb> Class.new.everywhere
=> true
irb> Class.only_singleton
=> true
irb> Class.new.only_singleton
NoMethodError: undefined method 'only_in_singleton' for #<Class:0x4800ac8>