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
This commit is contained in:
Sean Griffin 2016-12-01 14:53:31 -05:00
parent 2204233165
commit 4e73ffa9b4
2 changed files with 11 additions and 0 deletions

View File

@ -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

View File

@ -25,4 +25,14 @@ class ClassTest < ActiveSupport::TestCase
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