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
58 changes: 54 additions & 4 deletions internal/vm/argf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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
})
}
15 changes: 15 additions & 0 deletions internal/vm/argf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
2 changes: 1 addition & 1 deletion scripts/conformance/rubyspec/FLOOR
Original file line number Diff line number Diff line change
@@ -1 +1 @@
16150
16170