Class: Samplelines::MultiFile

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/samplelines.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filenames = []) ⇒ MultiFile

Returns a new instance of MultiFile.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/samplelines.rb', line 12

def initialize(filenames = [])
  self.filenames = filenames
  self.files = []
  if filenames.empty?
    filenames = ['STDIN']
  end
  
  filenames.each do |fn|
    case fn
    when 'STDIN'
      self.files.push $stdin
    when 'STDERR'
      self.files.push $stderr
    else
      f = File.open(fn, 'r:utf-8')
      if fn =~ /\.gz\Z/
        f = Zlib::GzipReader.new(f)
      end
      self.files.push f
    end
  end
end

Instance Attribute Details

#filenamesObject

Returns the value of attribute filenames.



10
11
12
# File 'lib/samplelines.rb', line 10

def filenames
  @filenames
end

#filesObject

Returns the value of attribute files.



10
11
12
# File 'lib/samplelines.rb', line 10

def files
  @files
end

Instance Method Details

#eachObject



36
37
38
39
40
# File 'lib/samplelines.rb', line 36

def each
  files.each do |f|
    f.each_line {|l| yield l}
  end
end