Ruby 1.8 vs.1.9:确定一个Ruby单例类是否是另一个类的子类
如中所述,Ruby 1.8 vs.1.9:确定一个Ruby单例类是否是另一个类的子类,ruby,inheritance,subclass,superclass,eigenclass,Ruby,Inheritance,Subclass,Superclass,Eigenclass,如中所述,B
B
是确定一个类是否是另一个类的子类的最简单方法。然而,在Ruby 1.8(也是REE1.8)中,当B
是一个单例类(即本征类)时,这似乎失败了。举例说明:
class A; end
class B < A; end
b = B.new
B.ancestors # => [B, A, Object, Kernel] (also BasicObject in 1.9)
(class << b; self; end).ancestors # => [B, A, Object, Kernel] (also BasicObject in 1.9)
B < A # => true
(class << b; self; end) < A # => true in 1.9; false in 1.8
有人知道这是1.8中的一个bug,碰巧在1.9中被修复了,还是在1.9中故意改变了预期的行为?我试着在其他地方找到关于这个问题的提及或文档,但没有找到任何东西
由于这个问题,有人知道检查singleton类是Ruby 1.8中a
的子类的最佳方法是什么吗?我一直在做(class我建议:
class Object; def metaclass; class << self; self; end; end; end
class A; end
class B < A; end
b = B.new
b.metaclass < A.metaclass || b.metaclass < A
class对象;def元类;类
class Object; def metaclass; class << self; self; end; end; end
class A; end
class B < A; end
b = B.new
b.metaclass < A.metaclass || b.metaclass < A