aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorSean Griffin <sean@seantheprogrammer.com>2016-12-01 14:53:31 -0500
committerSean Griffin <sean@seantheprogrammer.com>2016-12-01 14:53:31 -0500
commit4e73ffa9b45904492815f8f67d4695ef719e0350 (patch)
tree132461bb09747fd5a66fd40316578713a04c249c /activesupport
parent22042331650e943a1bb3f954fef7e2951e3ad2a3 (diff)
downloadrails-4e73ffa9b45904492815f8f67d4695ef719e0350.tar.gz
rails-4e73ffa9b45904492815f8f67d4695ef719e0350.tar.bz2
rails-4e73ffa9b45904492815f8f67d4695ef719e0350.zip
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
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/core_ext/class/subclasses.rb1
-rw-r--r--activesupport/test/core_ext/class_test.rb10
2 files changed, 11 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/core_ext/class/subclasses.rb b/activesupport/lib/active_support/core_ext/class/subclasses.rb
index 10a7c787f6..62397d9508 100644
--- a/activesupport/lib/active_support/core_ext/class/subclasses.rb
+++ b/activesupport/lib/active_support/core_ext/class/subclasses.rb
@@ -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
diff --git a/activesupport/test/core_ext/class_test.rb b/activesupport/test/core_ext/class_test.rb
index a9c44907cc..a7905196ae 100644
--- a/activesupport/test/core_ext/class_test.rb
+++ b/activesupport/test/core_ext/class_test.rb
@@ -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