Class: Readorder::Filelist
- Inherits:
-
Object
- Object
- Readorder::Filelist
- Defined in:
- lib/readorder/filelist.rb
Overview
An interator over the contents of a bunch of files or IO objects depending on the initializer.
Defined Under Namespace
Classes: Error
Instance Method Summary collapse
- #current_source ⇒ Object
-
#each_line ⇒ Object
Iterator yielding the line returned, stopping on no more lines.
-
#gets ⇒ Object
return the next line from the sources, opening a new source if need be.
-
#initialize(sources = []) ⇒ Filelist
constructor
A new instance of Filelist.
Constructor Details
#initialize(sources = []) ⇒ Filelist
Returns a new instance of Filelist.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/readorder/filelist.rb', line 9 def initialize( sources = [] ) @sources = [ sources ].flatten @current_source = nil @sources.each do |s| case s when String raise Error, "#{s} does not exist" unless File.exist?( s ) raise Error, "#{s} is not readable" unless File.readable?( s ) else [ :gets, :close ].each do |meth| raise Error, "#{s.inspect} does not respond to '#{meth}'" unless s.respond_to? meth end end end end |
Instance Method Details
#current_source ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/readorder/filelist.rb', line 25 def current_source if not @current_source then cs = @sources.shift case cs when String @current_source = File.open( cs ) else # nil or respond_to? :gets @current_source = cs end end return @current_source end |
#each_line ⇒ Object
Iterator yielding the line returned, stopping on no more lines
55 56 57 58 59 |
# File 'lib/readorder/filelist.rb', line 55 def each_line while line = self.gets do yield line end end |
#gets ⇒ Object
return the next line from the sources, opening a new source if need be
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/readorder/filelist.rb', line 41 def gets loop do return nil unless self.current_source line = self.current_source.gets return line if line @current_source.close unless @current_source == $stdin @current_source = nil end end |