From ad03e68c9b8e2e52811d9ed9e21ad9094706c59d Mon Sep 17 00:00:00 2001 From: tannevaled Date: Thu, 27 Aug 2026 11:05:46 +0200 Subject: [PATCH] Numeric: zero?, real, floor, ceil, round, truncate fallbacks A Numeric subclass that defines only #to_f (and #<=>) inherited none of these, so zero? / real / floor / ceil / round / truncate raised NoMethodError (and #nonzero?, which dispatches through #zero?, broke with it). They now delegate the way MRI does: zero? is self == 0 (honouring a subclass's own #== / #<=>), real returns self, and floor / ceil / round / truncate operate on #to_f, forwarding the optional digits argument to Float. Integer and Float keep their own overrides. Verified against ruby 4.0.6. Whole-package coverage stays at 100%; the ruby/spec floor rises 15800 -> 15810 (core/numeric +6). Co-Authored-By: Claude Opus 4.8 --- internal/vm/complex.go | 18 ++++++++++++++++++ internal/vm/numeric_test.go | 22 ++++++++++++++++++++++ scripts/conformance/rubyspec/FLOOR | 2 +- 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/internal/vm/complex.go b/internal/vm/complex.go index 183935f2..ecdaae19 100644 --- a/internal/vm/complex.go +++ b/internal/vm/complex.go @@ -145,6 +145,24 @@ func registerNumericGeneric(vm *VM, cNumeric *RClass) { cNumeric.define("real?", func(vm *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { return object.True }) + // zero? is self == 0, dispatched so a subclass's own #== / #<=> decides. This + // is also what the #nonzero? above relies on. + cNumeric.define("zero?", func(vm *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { + return object.Bool(vm.send(self, "==", []object.Value{object.IntValue(0)}, nil).Truthy()) + }) + // real returns self: a real number is its own real part. + cNumeric.define("real", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { + return self + }) + // floor / ceil / round / truncate fall back to operating on #to_f, so a Numeric + // subclass that only defines #to_f still rounds; the optional digits argument is + // forwarded to Float's own implementation. + for _, name := range []string{"floor", "ceil", "round", "truncate"} { + m := name + cNumeric.define(m, func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + return vm.send(vm.send(self, "to_f", nil, nil), m, args, nil) + }) + } } // asComplexVal coerces a real number to a Complex (zero imaginary part); a diff --git a/internal/vm/numeric_test.go b/internal/vm/numeric_test.go index 1be734b1..632457dd 100644 --- a/internal/vm/numeric_test.go +++ b/internal/vm/numeric_test.go @@ -27,3 +27,25 @@ func TestNumericHierarchy(t *testing.T) { } } } + +// TestNumericDefaultMethods covers the Numeric fallbacks — zero?, real, floor, +// ceil, round and truncate — that a subclass defining only #to_f (and #<=>) +// inherits, asserted against MRI Ruby 4.0.6. +func TestNumericDefaultMethods(t *testing.T) { + pre := "class FNum < Numeric; def initialize(v); @v = v; end; def v; @v; end; " + + "def <=>(o); @v <=> (o.is_a?(FNum) ? o.v : o); end; def coerce(o); [FNum.new(o), self]; end; " + + "def to_f; @v.to_f; end; end; " + cases := []struct{ src, want string }{ + {pre + `p [FNum.new(2.7).truncate, FNum.new(2.7).floor, FNum.new(2.7).ceil, FNum.new(2.7).round]`, "[2, 2, 3, 3]\n"}, + {pre + `p [FNum.new(-2.7).truncate, FNum.new(-2.7).floor, FNum.new(-2.7).ceil, FNum.new(-2.7).round]`, "[-2, -3, -2, -3]\n"}, + {pre + `p FNum.new(2.75).round(1)`, "2.8\n"}, + {pre + `p [FNum.new(0).zero?, FNum.new(5).zero?]`, "[true, false]\n"}, + {pre + `p FNum.new(5).real.v`, "5\n"}, + {pre + `p [FNum.new(0).nonzero?, FNum.new(5).nonzero?.v]`, "[nil, 5]\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/scripts/conformance/rubyspec/FLOOR b/scripts/conformance/rubyspec/FLOOR index d32ab209..30f2e268 100644 --- a/scripts/conformance/rubyspec/FLOOR +++ b/scripts/conformance/rubyspec/FLOOR @@ -1 +1 @@ -15800 +15810