Class: JoinedInput

Inherits:
Object
  • Object
show all
Defined in:
lib/pslm/joinedinput.rb

Overview

“joins” several input streams, so that #gets behaves as if they were a single stream

Instance Method Summary collapse

Constructor Details

#initialize(*ins) ⇒ JoinedInput

accepts any number of open IOs



8
9
10
# File 'lib/pslm/joinedinput.rb', line 8

def initialize(*ins)
  @streams = ins
end

Instance Method Details

#closeObject



34
35
36
37
# File 'lib/pslm/joinedinput.rb', line 34

def close
  @streams.each {|s| s.close }
  @streams = []
end

#closed?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/pslm/joinedinput.rb', line 39

def closed?
  @streams.empty?
end

#getsObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/pslm/joinedinput.rb', line 12

def gets
  if @streams.empty? then
    return nil
  end

  l = @streams.first.gets
  if l == nil
    @streams.shift
    return gets
  end

  unless l.end_with? "\n"
    l += "\n"
  end

  return l
end

#readObject



30
31
32
# File 'lib/pslm/joinedinput.rb', line 30

def read
  @streams.collect {|s| s.read }.join "\n"
end