Commit 0f937b64
0f937b6405e9Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · +2 added · ~2 modified
Description
Implement File.readlines
File.readlines(path) was unwired -- the constant-receiver dispatch
had no readlines arm, so it fell through to the generic call and
emitted 0 ("cannot resolve call to 'readlines'").
Route it to sp_str_lines(sp_file_read(path)): read the whole file, then split on "\n" keeping the terminator (CRuby's default $/). Splitting on real newlines is more correct than mirroring the stdin sp_readlines() path, which uses a 4096-byte fgets buffer and would break any line longer than 4095 chars.
- codegen: File.readlines(path[, chomp: true]) takes the first
positional arg as the path; a trailing keyword hash (
encoding:,chomp:) is not the path.chomp: trueroutes to sp_str_lines_chomp. - analyze: types it
str_arrayalongside read/binread.
File.readlines("f") # => ["a\n", "b\n"] (was 0) File.readlines("f", chomp: true) # => ["a", "b"] File.readlines("f", encoding: 'utf-8') # encoding accepted, ignored
A missing file yields [] rather than raising Errno::ENOENT, inherited from and consistent with File.read (sp_file_read returns "" when fopen fails); the broader ENOENT gap is out of scope here.
Fixes #1125
Co-Authored-By: Claude Opus 4.8 [email protected]
Details
- Commit
- 0f937b6405e9
- Parent
- ef8438164a86
spinel_analyze.rbmodifiedView file
if mname == "read" || mname == "binread" return "string" end if mname == "readlines" return "str_array" end if mname == "mtime" return "time" endspinel_codegen.rbmodifiedView file
Loading diff…
test/file_readlines.rbaddedView file
Loading diff…
test/file_readlines.rb.expectedaddedView file
Loading diff…