Permalink
Browse files

Exclude singleton classes from `subclasses` and `descendants`

This behavior changed in Ruby starting with 2.3.0, as a result of
https://bugs.ruby-lang.org/issues/11360. This results in a change in
behavior of these methods which is likely undesirable.

Fixes #27238
  • Loading branch information...
1 parent 2204233 commit 4e73ffa9b45904492815f8f67d4695ef719e0350 @sgrif sgrif committed Dec 1, 2016
@@ -22,6 +22,7 @@ class Class
def descendants
descendants = []
ObjectSpace.each_object(singleton_class) do |k|
+ next if k.singleton_class?
descendants.unshift k unless k == self
end
descendants
@@ -25,4 +25,14 @@ def test_subclasses
assert_equal [Baz], Bar.subclasses
assert_equal [], Baz.subclasses
end
+
+ def test_descendants_excludes_singleton_classes
+ klass = Parent.new.singleton_class
+ refute Parent.descendants.include?(klass), "descendants should not include singleton classes"
+ end
+
+ def test_subclasses_excludes_singleton_classes
+ klass = Parent.new.singleton_class
+ refute Parent.subclasses.include?(klass), "subclasses should not include singleton classes"
+ end
end

0 comments on commit 4e73ffa

Please sign in to comment.