Class: Async::IO::Protocol::Line

Inherits:
Object
  • Object
show all
Defined in:
lib/async/io/protocol/line.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stream, eol = $/) ⇒ Line

Returns a new instance of Line.



27
28
29
30
# File 'lib/async/io/protocol/line.rb', line 27

def initialize(stream, eol = $/)
  @stream = stream
  @eol = eol
end

Instance Attribute Details

#eolObject (readonly)

Returns the value of attribute eol.



37
38
39
# File 'lib/async/io/protocol/line.rb', line 37

def eol
  @eol
end

#streamObject (readonly)

Returns the value of attribute stream.



36
37
38
# File 'lib/async/io/protocol/line.rb', line 36

def stream
  @stream
end

Instance Method Details

#closeObject



32
33
34
# File 'lib/async/io/protocol/line.rb', line 32

def close
  @stream.close
end

#each_lineObject



66
67
68
69
70
71
72
# File 'lib/async/io/protocol/line.rb', line 66

def each_line
  return to_enum(:each_line) unless block_given?
  
  while line = @stream.read_until(@eol)
    yield line
  end
end

#peek_lineObject

Raises:

  • (EOFError)


56
57
58
59
60
61
62
63
64
# File 'lib/async/io/protocol/line.rb', line 56

def peek_line
  @stream.peek do |read_buffer|
    if index = read_buffer.index(@eol)
      return read_buffer.slice(0, index)
    end
  end
  
  raise EOFError
end

#read_lineObject



52
53
54
# File 'lib/async/io/protocol/line.rb', line 52

def read_line
  @stream.read_until(@eol) or @stream.eof!
end

#read_linesObject



74
75
76
# File 'lib/async/io/protocol/line.rb', line 74

def read_lines
  @stream.read.split(@eol)
end

#write_lines(*args) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/async/io/protocol/line.rb', line 39

def write_lines(*args)
  if args.empty?
    @stream.write(@eol)
  else
    args.each do |arg|
      @stream.write(arg)
      @stream.write(@eol)
    end
  end
  
  @stream.flush
end