From 6ce33138e9b17eb80fc703ce5e63c88668300c9b Mon Sep 17 00:00:00 2001 From: tannevaled Date: Sun, 30 Aug 2026 10:05:17 +0200 Subject: [PATCH] Add Class#subclasses Class#subclasses returns the classes that directly inherit from the receiver (in no particular order), excluding singleton classes and any included or prepended modules. Named classes are now recorded in the live-class table as they are defined (Class.new already was), and #subclasses walks it filtering on the direct superclass. Verified against ruby 4.0.6. Whole-package coverage stays at 100%; the ruby/spec floor rises 16270 -> 16280 (core/class/subclasses 0 -> 8). Co-Authored-By: Claude Opus 4.8 --- internal/vm/builtins.go | 13 +++++++++++++ internal/vm/reflection_methods_test.go | 19 +++++++++++++++++++ internal/vm/vm.go | 1 + scripts/conformance/rubyspec/FLOOR | 2 +- 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/internal/vm/builtins.go b/internal/vm/builtins.go index 92e2aa45..f620f7a6 100644 --- a/internal/vm/builtins.go +++ b/internal/vm/builtins.go @@ -5015,6 +5015,19 @@ func (vm *VM) bootstrap() { } return object.NilV }) + // Class#subclasses: the classes that directly inherit from self (in no + // particular order), excluding singleton classes and any included/prepended + // modules. Walks the live-class table populated as classes are defined. + vm.cClass.define("subclasses", func(vm *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { + parent := self.(*RClass) + out := object.NewArray() + for _, w := range vm.liveClasses { + if c := w.Value(); c != nil && c.super == parent && !c.isModule && !c.isSingleton { + out.Elems = append(out.Elems, c) + } + } + return out + }) vm.cInteger.define("step", func(vm *VM, self object.Value, args []object.Value, blk *Proc) object.Value { limit, step := vm.stepBounds(args) diff --git a/internal/vm/reflection_methods_test.go b/internal/vm/reflection_methods_test.go index 9bf61e20..a01ff9d9 100644 --- a/internal/vm/reflection_methods_test.go +++ b/internal/vm/reflection_methods_test.go @@ -70,3 +70,22 @@ func TestModuleReflectionC37(t *testing.T) { } } } + +// TestClassSubclasses covers Class#subclasses: the classes directly inheriting +// from self, excluding included/prepended modules and deeper descendants. +// Asserted against MRI Ruby 4.0.6. +func TestClassSubclasses(t *testing.T) { + cases := []struct{ src, want string }{ + {`class SubA; end; class SubB < SubA; end; class SubC < SubA; end; p SubA.subclasses.map(&:name).sort`, "[\"SubB\", \"SubC\"]\n"}, + {`p Class.new.subclasses`, "[]\n"}, + {`class SubD; end; class SubE < SubD; end; class SubF < SubE; end; p SubD.subclasses.map(&:name)`, "[\"SubE\"]\n"}, + // A module mixed into the parent is not a subclass. + {`parent = Class.new; child = Class.new(parent); parent.include(Module.new); p parent.subclasses == [child]`, "true\n"}, + {`parent = Class.new; child = Class.new(parent); parent.prepend(Module.new); p parent.subclasses == [child]`, "true\n"}, + } + for _, c := range cases { + if got := eval(t, c.src); got != c.want { + t.Errorf("src=%q got=%q want=%q", c.src, got, c.want) + } + } +} diff --git a/internal/vm/vm.go b/internal/vm/vm.go index 414e9094..9675c7cd 100644 --- a/internal/vm/vm.go +++ b/internal/vm/vm.go @@ -2225,6 +2225,7 @@ func (vm *VM) defineClassIn(parent *RClass, name string, body *bytecode.ISeq, su super = sc.(*RClass) } class = newClass(scopedNameFor(parent, name), super) + vm.registerLiveClass(class) // so super.subclasses (and ObjectSpace) can find it // A compact (scoped) definition's lexical nesting is only itself, so its // lexParent terminates the chain (nil); a bare nested definition records its // enclosing scope so the body sees the surrounding namespace. diff --git a/scripts/conformance/rubyspec/FLOOR b/scripts/conformance/rubyspec/FLOOR index 54829da3..8ec23a6b 100644 --- a/scripts/conformance/rubyspec/FLOOR +++ b/scripts/conformance/rubyspec/FLOOR @@ -1 +1 @@ -16270 +16280