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
30 changes: 27 additions & 3 deletions internal/vm/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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")
Expand All @@ -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)
Expand All @@ -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
Expand Down
27 changes: 27 additions & 0 deletions internal/vm/io_gets_globals_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
2 changes: 1 addition & 1 deletion scripts/conformance/rubyspec/FLOOR
Original file line number Diff line number Diff line change
@@ -1 +1 @@
17300
17310