From 8cc53f4e935f09917a3c342ad6d97ea46b70adcf Mon Sep 17 00:00:00 2001 From: Tsutomu Katsube Date: Mon, 16 Nov 2026 20:40:30 +0900 Subject: [PATCH] Add RemoteInput#each_line GitHub: GH-5 It's like `IO#each_line`. It passes all arguments and the block to `IO#each_line`. It also returns the same value as `IO#each_line` does: * With a block: returns `self`, not the internal `File` * Without a block: returns an `Enumerator` We need this to read a `RemoteInput` line by line, because Red Datasets reads some text data with `IO#each_line`: ```ruby RemoteInput.open("https://example.com/data.txt") do |input| input.each_line(chomp: true) do |line| # ... end end ``` --- lib/remote_input.rb | 6 ++++++ test/test-remote-input.rb | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/lib/remote_input.rb b/lib/remote_input.rb index 3e480f6..4c21f97 100644 --- a/lib/remote_input.rb +++ b/lib/remote_input.rb @@ -47,6 +47,12 @@ def gets(...) local_file.gets(...) end + def each_line(...) + return to_enum(__method__, ...) unless block_given? + local_file.each_line(...) + self + end + def each_chunk(&block) return to_enum(__method__) unless block_given? download(&block) diff --git a/test/test-remote-input.rb b/test/test-remote-input.rb index d6a8378..6a21e2b 100644 --- a/test/test-remote-input.rb +++ b/test/test-remote-input.rb @@ -111,6 +111,42 @@ def test_gets_after_close end end + def test_each_line + open_input do |input| + input.send(:cache_path).parent.mkpath + input.send(:cache_path).write("1\n2\n") + assert_equal(["1\n", "2\n"], + input.each_line.to_a) + end + end + + def test_each_line_with_nil_separator + open_input do |input| + input.send(:cache_path).parent.mkpath + input.send(:cache_path).write("1\n2\n") + assert_equal(["1\n2\n"], + input.each_line(nil).to_a) + end + end + + def test_each_line_with_chomp + open_input do |input| + input.send(:cache_path).parent.mkpath + input.send(:cache_path).write("1\n2\n") + assert_equal(["1", "2"], + input.each_line(chomp: true).to_a) + end + end + + def test_each_line_return_self + open_input do |input| + input.send(:cache_path).parent.mkpath + input.send(:cache_path).write("1\n2\n") + assert_same(input, + input.each_line {}) + end + end + def test_each_chunk open_input do |input| expected_chunks = ["1" * (1024 * 1024), "2"]