Feature #5129

Create a core class "FileArray" and make "ARGF" its instance

Added by Joey Zhou over 4 years ago. Updated about 3 years ago.

Assignee:Yukihiro Matsumoto


I suggest to create a class "FileArray" whose instance behaves just like ARGF do.
And I think ARGF should be an instance of FileArray. Now when I "p ARGF.class", I get "ARGF.class", so ARGF is an instance of ARGF.class, how meaningless it is.

FileArray methods:

create an instance

fa = FileArray.new('a.txt','b.txt','c.txt')

take many methods from IO

most methods from ARGF should be instance methods of ARGF

fa.each {|line| puts line }
fa.filename # current file

but "argv" not

p fa.file_list # in ARGF, its ARGF.argv, but #argv is not a proper name for FileArray

ARGV array can be modified, adding new file into it, all replace to a new file list.

FileArray should add some methods to modify the inner file list.


With FileArray, You can create multiple ARGF-like file arrays simultaneously.

For example, I want to mix two groups of files, not two files:

a_files = FileArray.new(Dir.glob('a.txt'))
b_files = FileArray.new(Dir.glob('b.txt'))

enum_a = a_files.each
enum_b = b_files.each

loop do
puts enum_a.next
puts enum_b.next


#1 Updated by Yusuke Endoh over 3 years ago

  • Status changed from Open to Assigned
  • Assignee set to Yukihiro Matsumoto


I like the idea, but it requires matz's approval.

Now when I "p ARGF.class", I get "ARGF.class", so ARGF is an instance of ARGF.class, how meaningless it is.

FYI: In fact, it is meaningful. You can "exploit" it.

$ cat a.txt
$ cat b.txt
$ ruby -e 'p ARGF.class.new("a.txt", "b.txt").read'

I'm not sure if this behavior is the spec, though.

Yusuke Endoh mame@tsg.ne.jp

#2 Updated by Yusuke Endoh about 3 years ago

  • Target version set to next minor

Also available in: Atom PDF