From 920b6876f1319ba67f84bafd49a95a3051930b14 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Fri, 21 Aug 2026 16:20:51 +0200 Subject: [PATCH] Implement Numeric#coerce for subclasses MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Numeric#coerce was defined only on Integer and Float, so a user Numeric subclass did not inherit #coerce: N.new.respond_to?(:coerce) was false (MRI: true), and code relying on the inherited method — for example undef_method :coerce on a Numeric subclass — raised NameError. Numeric#coerce is now defined on Numeric itself (Integer/Float keep their overrides): a same-class argument returns [other, self] unchanged; any other argument pairs both operands converted with Kernel#Float (so #to_f is dispatched, and a non-convertible value raises the same ArgumentError/TypeError Float() would), converting self first to match MRI's error precedence. Verified against ruby 4.0.6. Co-Authored-By: Claude Opus 4.8 --- internal/vm/builtins.go | 14 ++++++++ internal/vm/numeric_coerce_internal_test.go | 36 +++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 internal/vm/numeric_coerce_internal_test.go diff --git a/internal/vm/builtins.go b/internal/vm/builtins.go index 2a769534..7ccaf884 100644 --- a/internal/vm/builtins.go +++ b/internal/vm/builtins.go @@ -4477,6 +4477,20 @@ func (vm *VM) bootstrap() { } vm.cInteger.define("coerce", coerce) vm.cFloat.define("coerce", coerce) + // Numeric#coerce is the generic fallback a Numeric subclass inherits (Integer + // and Float override it above): same-class returns [other, self] unchanged, + // otherwise both are converted with Kernel#Float (dispatching #to_f, and + // raising the same errors as Float() for a non-convertible value). self is + // converted first, matching MRI's error precedence. + cNumeric.define("coerce", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + other := args[0] + if vm.classOf(self) == vm.classOf(other) { + return object.NewArray(other, self) + } + sf := vm.send(vm.main, "Float", []object.Value{self}, nil) + of := vm.send(vm.main, "Float", []object.Value{other}, nil) + return object.NewArray(of, sf) + }) vm.cInteger.define("bit_length", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { n := intOf(self) if n < 0 { diff --git a/internal/vm/numeric_coerce_internal_test.go b/internal/vm/numeric_coerce_internal_test.go new file mode 100644 index 00000000..c7369728 --- /dev/null +++ b/internal/vm/numeric_coerce_internal_test.go @@ -0,0 +1,36 @@ +// Copyright (c) the go-embedded-ruby/ruby authors +// +// SPDX-License-Identifier: BSD-3-Clause + +package vm + +import "testing" + +// TestNumericCoerce covers the generic Numeric#coerce a Numeric subclass +// inherits (Integer/Float override it): a same-class argument is returned +// paired unchanged, a different-class argument pairs both operands converted +// with Kernel#Float (via #to_f), a non-convertible argument raises the same +// error Float() would, and #coerce being inherited means undef_method :coerce +// now works on a Numeric subclass. Verified against ruby 4.0.6. +func TestNumericCoerce(t *testing.T) { + const def = "class N < Numeric\n def initialize(v); @v = v; end\n def to_f; @v.to_f; end\nend\n" + cases := []struct{ src, want string }{ + // A Numeric subclass inherits #coerce. + {def + `p N.new(3).respond_to?(:coerce)`, `true`}, + // Different class: both converted to Float (via #to_f). + {def + `p N.new(3).coerce(5)`, `[5.0, 3.0]`}, + {def + `p N.new(3).coerce(2.5)`, `[2.5, 3.0]`}, + // Same class: returned as the pair [other, self] unchanged. + {def + `a = N.new(3); b = N.new(5); p a.coerce(b).map(&:to_f)`, `[5.0, 3.0]`}, + // A non-convertible argument raises what Float() would. + {def + `begin; N.new(3).coerce("x"); rescue ArgumentError => e; p e.class; end`, `ArgumentError`}, + {def + `begin; N.new(3).coerce(nil); rescue TypeError => e; p e.class; end`, `TypeError`}, + // Because #coerce is now inherited, undef_method :coerce succeeds. + {`class M < Numeric; undef_method :coerce; end; p M.new.respond_to?(:coerce)`, `false`}, + } + for _, c := range cases { + if got := eval(t, c.src); got != c.want+"\n" { + t.Errorf("src=%q got=%q want=%q", c.src, got, c.want+"\n") + } + } +}