Skip to content

Commit 4e73ffa

Browse files
committed
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 rails#27238
1 parent 2204233 commit 4e73ffa

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

activesupport/lib/active_support/core_ext/class/subclasses.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class Class
2222
def descendants
2323
descendants = []
2424
ObjectSpace.each_object(singleton_class) do |k|
25+
next if k.singleton_class?
2526
descendants.unshift k unless k == self
2627
end
2728
descendants

activesupport/test/core_ext/class_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,14 @@ def test_subclasses
2525
assert_equal [Baz], Bar.subclasses
2626
assert_equal [], Baz.subclasses
2727
end
28+
29+
def test_descendants_excludes_singleton_classes
30+
klass = Parent.new.singleton_class
31+
refute Parent.descendants.include?(klass), "descendants should not include singleton classes"
32+
end
33+
34+
def test_subclasses_excludes_singleton_classes
35+
klass = Parent.new.singleton_class
36+
refute Parent.subclasses.include?(klass), "subclasses should not include singleton classes"
37+
end
2838
end

0 commit comments

Comments
 (0)