Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions internal/vm/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 19 additions & 0 deletions internal/vm/reflection_methods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
1 change: 1 addition & 0 deletions internal/vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion scripts/conformance/rubyspec/FLOOR
Original file line number Diff line number Diff line change
@@ -1 +1 @@
16270
16280