From 4ac507a26e98de44c3b3aacd1d451e11e5479cff Mon Sep 17 00:00:00 2001 From: tannevaled Date: Mon, 24 Aug 2026 10:18:40 +0200 Subject: [PATCH] String#%: convert the argument with #to_ary MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit String#% only spread an Array argument and otherwise treated the whole right-hand side as a single value, so `"%d %d" % obj` where obj responds to #to_ary raised "can't convert Object into Integer" (too few arguments) instead of using the converted array. The argument is now turned into the positional list by a VM-aware vmFormatArgs: an Array spreads, a Hash is kept whole for %{name}/% references, and any other object is converted with #to_ary when it responds — an Array result spreads, a nil result falls back to a single argument, and a non-Array result raises the MRI TypeError ("can't convert X to Array (X#to_ary gives Y)"); a scalar with no #to_ary stays a single argument. The old location-agnostic formatArgs (now unused) is removed. Verified against ruby 4.0.6; core/string/modulo_spec 235 -> 245. Co-Authored-By: Claude Opus 4.8 --- internal/vm/arith.go | 2 +- internal/vm/format_bind.go | 24 +++++++++++++-- internal/vm/format_to_ary_internal_test.go | 36 ++++++++++++++++++++++ 3 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 internal/vm/format_to_ary_internal_test.go diff --git a/internal/vm/arith.go b/internal/vm/arith.go index 19c37c2f..cbb3c313 100644 --- a/internal/vm/arith.go +++ b/internal/vm/arith.go @@ -316,7 +316,7 @@ func (vm *VM) binaryOp(op bytecode.Op, a, b object.Value) object.Value { // %f#to_f, %{name}#to_s), so route it through the VM-aware formatter rather // than the VM-less stringOp path. if as, ok := a.(*object.String); ok && op == bytecode.OpMod { - return object.NewString(vm.formatString(as.Str(), formatArgs(b))) + return object.NewString(vm.formatString(as.Str(), vm.vmFormatArgs(b))) } // A user object (RObject with no builtin backing) that defines an // arithmetic operator dispatches to it, so `Pathname + str`, a Money `+`, diff --git a/internal/vm/format_bind.go b/internal/vm/format_bind.go index ec3c891e..0f47a2a6 100644 --- a/internal/vm/format_bind.go +++ b/internal/vm/format_bind.go @@ -299,9 +299,27 @@ func raiseFormatError(err error) { // stays a one-element argument list, which formatString both treats as a // positional operand and exposes as the %/%{name} hash (MRI's behaviour // where "%d %s" % {a: 1} formats the hash for both forms). -func formatArgs(b object.Value) []object.Value { - if arr, ok := b.(*object.Array); ok { - return arr.Elems +// vmFormatArgs turns the right-hand side of String#% into the positional +// argument list. An Array is spread; a Hash is kept whole (for %{name}/% +// references); any other object is converted with #to_ary when it responds — +// an Array result is spread, a nil result falls back to a single argument, and a +// non-Array result raises TypeError, as MRI does; otherwise it is a single +// argument. +func (vm *VM) vmFormatArgs(b object.Value) []object.Value { + switch b.(type) { + case *object.Array: + return b.(*object.Array).Elems + case *object.Hash: + return []object.Value{b} + } + if vm.respondsToDynamic(b, "to_ary") { + if r := vm.send(b, "to_ary", nil, nil); !object.IsNil(r) { + if arr, ok := r.(*object.Array); ok { + return arr.Elems + } + raise("TypeError", "can't convert %s to Array (%s#to_ary gives %s)", + vm.classOf(b).name, vm.classOf(b).name, vm.classOf(r).name) + } } return []object.Value{b} } diff --git a/internal/vm/format_to_ary_internal_test.go b/internal/vm/format_to_ary_internal_test.go new file mode 100644 index 00000000..2c4c2e74 --- /dev/null +++ b/internal/vm/format_to_ary_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" + +// TestFormatToAry covers how String#% turns its right-hand side into the +// positional argument list: an Array is spread, a Hash is kept whole for +// %{name}/% references, a single scalar is one argument, and any other +// object is converted with #to_ary (an Array spreads, a nil result falls back to +// a single argument, a non-Array result raises TypeError). Verified against +// ruby 4.0.6. +func TestFormatToAry(t *testing.T) { + cases := []struct{ src, want string }{ + // Array spreads; a plain scalar is a single argument. + {`p "%d %d" % [1, 2]`, `"1 2"`}, + {`p "%d" % 5`, `"5"`}, + // A Hash stays whole (named references / %s of the hash). + {`p "%d" % {x: 7}`, `"7"`}, + {`p "%s" % {a: 1}`, `"{a: 1}"`}, + // #to_ary returning an Array is spread. + {`class FA; def to_ary; [1, 2]; end; end; p "%d %d" % FA.new`, `"1 2"`}, + // #to_ary returning nil falls back to a single argument. + {`class FB; def to_ary; nil; end; def to_s; "b"; end; end; p "%s" % FB.new`, `"b"`}, + // #to_ary returning a non-Array raises TypeError. + {`class FC; def to_ary; 5; end; end +begin; "%s" % FC.new; rescue TypeError => e; p e.message; end`, `"can't convert FC to Array (FC#to_ary gives Integer)"`}, + } + 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") + } + } +}