From dccdff19bfbb9f693fa947c072a7c699c3dfc959 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Mon, 31 Aug 2026 12:16:38 +0200 Subject: [PATCH] Maintain $. and $_ across the IO#gets line-reader family IO#gets / #readline advanced the IOObj's own #lineno but never updated the $. (line number) and $_ (last read line) globals, so a spec (or program) that inspects them after a read saw the stale value. Now gets and readline set both $. and $_ (a StringIO updates them too, matching MRI); reading past the last line clears $_ while $. keeps the final number; and the bulk readers #readlines / #each_line advance $. but leave $_ untouched, as MRI does. core/io/gets_spec 21 -> 26, readline_spec 9 -> 10, each_line_spec 18 -> 19, no regressions. ruby/spec ratchet FLOOR 17300 -> 17310 (measured 17351). Co-Authored-By: Claude Opus 4.8 --- internal/vm/io.go | 30 ++++++++++++++++++++++++++--- internal/vm/io_gets_globals_test.go | 27 ++++++++++++++++++++++++++ scripts/conformance/rubyspec/FLOOR | 2 +- 3 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 internal/vm/io_gets_globals_test.go diff --git a/internal/vm/io.go b/internal/vm/io.go index d56d0696..86090889 100644 --- a/internal/vm/io.go +++ b/internal/vm/io.go @@ -759,10 +759,32 @@ func defStringIORead(cls *RClass) { o.pos += sz return s }) - gets := func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + // setLineGlobals records a successful line read the way MRI's IO#gets family + // does. $. always becomes the reading IO's line number (a StringIO updates it + // too). $_ (the "last read line") is set only by the single-line readers — + // gets/readline (lastLine true) — not by the bulk readlines/each_line, which + // leave $_ alone, matching MRI. + setLineGlobals := func(vm *VM, o *IOObj, v object.Value, lastLine bool) { + if v == object.NilV { + return + } + vm.globals["$."] = object.IntValue(int64(o.lineno)) + if lastLine { + vm.globals["$_"] = v + } + } + gets := func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { o := self.(*IOObj) ioCheckReadable(o) - return ioGets(o, args) + v := ioGets(o, args) + if v == object.NilV { + // Reading past the last line clears $_ (but leaves $. reporting the + // final line number), matching MRI. + vm.globals["$_"] = object.NilV + } else { + setLineGlobals(vm, o, v, true) + } + return v } cls.define("gets", gets) cls.define("readline", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { @@ -772,7 +794,7 @@ func defStringIORead(cls *RClass) { } return v }) - cls.define("readlines", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + cls.define("readlines", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { o := self.(*IOObj) ioCheckReadable(o) checkGetsLimit(args, "readlines") @@ -782,6 +804,7 @@ func defStringIORead(cls *RClass) { if v == object.NilV { break } + setLineGlobals(vm, o, v, false) lines = append(lines, v) } return object.NewArrayFromSlice(lines) @@ -795,6 +818,7 @@ func defStringIORead(cls *RClass) { if v == object.NilV { break } + setLineGlobals(vm, o, v, false) vm.callBlock(blk, []object.Value{v}) } return self diff --git a/internal/vm/io_gets_globals_test.go b/internal/vm/io_gets_globals_test.go new file mode 100644 index 00000000..fb45b999 --- /dev/null +++ b/internal/vm/io_gets_globals_test.go @@ -0,0 +1,27 @@ +package vm_test + +import "testing" + +// TestGetsLineGlobals covers the $. (line number) and $_ (last read line) globals +// that IO#gets / #readline / #each_line / #readlines maintain, verified against +// MRI Ruby 4.0.6: gets and readline set both $. and $_; the bulk readers set $. +// but leave $_ untouched; and reading past the end clears $_. +func TestGetsLineGlobals(t *testing.T) { + cases := []struct{ src, want string }{ + // gets sets $. to the line number and $_ to the line (a StringIO too). + {`require "stringio"; io = StringIO.new("a\nb\nc\n"); io.gets; p [$., $_]`, "[1, \"a\\n\"]\n"}, + {`require "stringio"; io = StringIO.new("a\nb\n"); io.gets; io.gets; p $.`, "2\n"}, + {`require "stringio"; io = StringIO.new("x\n"); p io.readline; p [$., $_]`, "\"x\\n\"\n[1, \"x\\n\"]\n"}, + // Reading past the last line clears $_ but keeps $. at the final number. + {`require "stringio"; io = StringIO.new("a\n"); io.gets; io.gets; p [$., $_]`, "[1, nil]\n"}, + // readlines advances $. to the count but does not touch $_. + {`require "stringio"; io = StringIO.new("a\nb\nc\n"); $_ = "pre"; io.readlines; p [$., $_]`, "[3, \"pre\"]\n"}, + // each_line advances $. but leaves the outer $_ alone. + {`require "stringio"; io = StringIO.new("a\nb\n"); $_ = "pre"; io.each_line {}; p [$., $_]`, "[2, \"pre\"]\n"}, + } + for _, c := range cases { + if got := eval(t, c.src); got != c.want { + t.Errorf("src=%q\n got=%q\nwant=%q", c.src, got, c.want) + } + } +} diff --git a/scripts/conformance/rubyspec/FLOOR b/scripts/conformance/rubyspec/FLOOR index 7d75b0ab..dd895343 100644 --- a/scripts/conformance/rubyspec/FLOOR +++ b/scripts/conformance/rubyspec/FLOOR @@ -1 +1 @@ -17300 +17310