From bda9b13c1472924efbdda475294cea85c9b2cdd9 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Sat, 29 Aug 2026 12:35:45 +0200 Subject: [PATCH] ARGF: per-unit iterators, byte reads, file, and true method aliases Extend ARGF with the character/byte iteration protocol and tidy its aliases: - each_byte / each_char / each_codepoint yield every unit across the files (an Enumerator with no block), and getbyte / readbyte read a single byte. - file returns the IO of the file currently being read. - eof, path, to_a and each are now true aliases of eof?, filename, readlines and each_line (sharing the method record), so MRI's Method-equality specs pass. Verified against ruby 4.0.6. Whole-package coverage stays at 100%; the ruby/spec floor rises 16150 -> 16170 (core/argf 42 -> 61). fileno/to_i, seek/pos/rewind and set_encoding still await the fd layer. Co-Authored-By: Claude Opus 4.8 --- internal/vm/argf.go | 58 +++++++++++++++++++++++++++--- internal/vm/argf_test.go | 15 ++++++++ scripts/conformance/rubyspec/FLOOR | 2 +- 3 files changed, 70 insertions(+), 5 deletions(-) diff --git a/internal/vm/argf.go b/internal/vm/argf.go index 0c440412..a730bb00 100644 --- a/internal/vm/argf.go +++ b/internal/vm/argf.go @@ -183,7 +183,7 @@ func (vm *VM) registerARGF() { } } d("each_line", eachLine) - d("each", eachLine) + aliasBuiltin(cls, "each", "each_line") // readlines / to_a: every remaining line as an Array. readlines := func(vm *VM, v object.Value, args []object.Value, _ *Proc) object.Value { @@ -197,14 +197,14 @@ func (vm *VM) registerARGF() { } } d("readlines", readlines) - d("to_a", readlines) + aliasBuiltin(cls, "to_a", "readlines") // eof? / eof: true when the current stream is exhausted and no file remains. eof := func(vm *VM, v object.Value, _ []object.Value, _ *Proc) object.Value { return object.Bool(self(v).ensure() == nil) } d("eof?", eof) - d("eof", eof) + aliasBuiltin(cls, "eof", "eof?") // lineno / lineno=: the cumulative line number ($.). d("lineno", func(vm *VM, v object.Value, _ []object.Value, _ *Proc) object.Value { @@ -225,7 +225,7 @@ func (vm *VM) registerARGF() { return object.NewString(a.curName) } d("filename", filename) - d("path", filename) + aliasBuiltin(cls, "path", "filename") // to_io / file: the underlying IO of the current file. d("to_io", func(vm *VM, v object.Value, _ []object.Value, _ *Proc) object.Value { @@ -272,4 +272,54 @@ func (vm *VM) registerARGF() { } return c }) + // getbyte / readbyte: the next byte across the files. + d("getbyte", func(vm *VM, v object.Value, _ []object.Value, _ *Proc) object.Value { + o := self(v).ensure() + if o == nil { + return object.NilV + } + return vm.send(o, "getbyte", nil, nil) + }) + d("readbyte", func(vm *VM, v object.Value, _ []object.Value, _ *Proc) object.Value { + b := vm.send(v, "getbyte", nil, nil) + if object.IsNil(b) { + raise("EOFError", "end of file reached") + } + return b + }) + + // each_byte / each_char / each_codepoint yield every unit across the files; + // with no block each returns an Enumerator. + iter := func(name, one string, conv func(vm *VM, c object.Value) object.Value) NativeFn { + return func(vm *VM, v object.Value, _ []object.Value, blk *Proc) object.Value { + if blk == nil { + return enumFor(v, name) + } + for { + c := vm.send(v, one, nil, nil) + if object.IsNil(c) { + return v + } + vm.callBlock(blk, []object.Value{conv(vm, c)}) + } + } + } + same := func(_ *VM, c object.Value) object.Value { return c } + d("each_byte", iter("each_byte", "getbyte", same)) + d("each_char", iter("each_char", "getc", same)) + d("each_codepoint", iter("each_codepoint", "getc", func(vm *VM, c object.Value) object.Value { + return vm.send(c, "ord", nil, nil) + })) + + // file: the IO of the file currently being read. + d("file", func(vm *VM, v object.Value, _ []object.Value, _ *Proc) object.Value { + a := self(v) + if !a.started { + a.ensure() + } + if a.cur != nil { + return a.cur + } + return object.NilV + }) } diff --git a/internal/vm/argf_test.go b/internal/vm/argf_test.go index f12b2cb6..a6a51628 100644 --- a/internal/vm/argf_test.go +++ b/internal/vm/argf_test.go @@ -47,6 +47,21 @@ end` // The singleton ARGF draws from ARGV, and #argv returns it. {with(`ARGV.replace([f1, f2]); p [ARGF.read, ARGF.argv]`), "[\"l1\\nl2\\nl3\\n\", []]\n"}, {`p ARGF.class.new.class.name`, "\"ARGF.class\"\n"}, + // each_char / each_byte / each_codepoint, with a block and as Enumerators. + {with(`a = ARGF.class.new(f2); p a.each_char.to_a`), "[\"l\", \"3\", \"\\n\"]\n"}, + {with(`a = ARGF.class.new(f2); out = []; a.each_char { |c| out << c }; p out`), "[\"l\", \"3\", \"\\n\"]\n"}, + {with(`a = ARGF.class.new(f2); p a.each_byte.to_a`), "[108, 51, 10]\n"}, + {with(`a = ARGF.class.new(f2); p a.each_codepoint.to_a`), "[108, 51, 10]\n"}, + // getbyte / readbyte. + {with(`a = ARGF.class.new(f2); p [a.getbyte, a.getbyte]`), "[108, 51]\n"}, + {with(`a = ARGF.class.new(f2); a.read; p (a.readbyte rescue :eof)`), ":eof\n"}, + // file: the current file's IO, nil once every input is consumed. + {with(`a = ARGF.class.new(f1); p a.file.is_a?(IO)`), "true\n"}, + {with(`a = ARGF.class.new(f1); a.close; p a.file`), "nil\n"}, + {with(`p ARGF.class.new(f2).readbyte`), "108\n"}, + // eof / path / to_a / each are true aliases of eof? / filename / readlines / + // each_line. + {`p [ARGF.class.instance_method(:eof) == ARGF.class.instance_method(:eof?), ARGF.class.instance_method(:path) == ARGF.class.instance_method(:filename), ARGF.class.instance_method(:to_a) == ARGF.class.instance_method(:readlines)]`, "[true, true, true]\n"}, // Native display / truthiness (ToS via interpolation, Inspect via Array, Truthy). {with(`p "x#{ARGF.class.new(f1)}"`), "\"xARGF\"\n"}, {with(`p [ARGF.class.new(f1)]`), "[ARGF]\n"}, diff --git a/scripts/conformance/rubyspec/FLOOR b/scripts/conformance/rubyspec/FLOOR index accc2d5c..ff8cb537 100644 --- a/scripts/conformance/rubyspec/FLOOR +++ b/scripts/conformance/rubyspec/FLOOR @@ -1 +1 @@ -16150 +16170