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:
parent
2204233165
commit
4e73ffa9b4
|
@ -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 @@ 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
|
||||
|
|
Loading…
Reference in New Issue