forked from ruby/ruby
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
ARGF.each_file
for iterating file/io objects
This makes it possible to iterate through `ARGF` file objects without having to call `ARGF.file` and `ARGF.skip` directly. `ARGF` already has a number of iteration helpers but nothing that exposes the underlying file objects. This can be useful when you want to operate on file arguments as separate files instead of as a single string or line-by-line. There's probably a more direct approach than using `yield_self` but for now it seemed best to keep things consistent with the other similar `ARGF` methods (eg, `argf_each_codepoint`).
- Loading branch information
1 parent
32cc630
commit cc8054c
Showing
3 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
require_relative '../../spec_helper' | ||
require_relative 'shared/each_file' | ||
|
||
describe "ARGF.each_file" do | ||
it_behaves_like :argf_each_file, :each_file | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
describe :argf_each_file, shared: true do | ||
before :each do | ||
@file1_name = fixture __FILE__, "file1.txt" | ||
@file2_name = fixture __FILE__, "file2.txt" | ||
@filenames = [@file1_name, @file2_name] | ||
|
||
@content = File.read(@file1_name) | ||
@content += File.read(@file2_name) | ||
end | ||
|
||
it "is a public method" do | ||
argf @filenames do | ||
@argf.public_methods(false).should include(@method) | ||
end | ||
end | ||
|
||
it "does not require arguments" do | ||
argf @filenames do | ||
@argf.method(@method).arity.should == 0 | ||
end | ||
end | ||
|
||
it "returns self when passed a block" do | ||
argf @filenames do | ||
@argf.send(@method) {}.should equal(@argf) | ||
end | ||
end | ||
|
||
it "yields each file of all streams" do | ||
argf @filenames do | ||
filenames = [] | ||
content = '' | ||
@argf.send(@method) do |file| | ||
filenames << file.path | ||
content += file.read | ||
end | ||
filenames.should == @filenames | ||
content.should == @content | ||
end | ||
end | ||
|
||
describe "stdin" do | ||
before :each do | ||
@stdin_name = fixture __FILE__, "stdin.txt" | ||
@stdin = File.read(@stdin_name) | ||
end | ||
|
||
it "yields each file and stdin" do | ||
stdin = ruby_exe("print ARGF.each_file.map(&:read).join", args: "#{@file1_name} #{@file2_name} - < #{@stdin_name}") | ||
stdin.should == @content + @stdin | ||
end | ||
|
||
it "uses stdin as default" do | ||
stdin = ruby_exe("print ARGF.each_file.map(&:read).join", args: "< #{@stdin_name}") | ||
stdin.should == @stdin | ||
end | ||
end | ||
|
||
describe "when no block is given" do | ||
it "returns an Enumerator" do | ||
argf @filenames do | ||
@argf.send(@method).should be_an_instance_of(Enumerator) | ||
end | ||
end | ||
|
||
describe "returned Enumerator" do | ||
describe "size" do | ||
it "should return nil" do | ||
argf @filenames do | ||
@argf.send(@method).size.should == nil | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |