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

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

Constant Summary

Constants inherited from Generic

Generic::WRAPPERS

Instance Attribute Summary collapse

Attributes inherited from Generic

#timeout

Instance Method Summary collapse

Methods inherited from Generic

#<<, #connected?, #dup, #nonblock, #nonblock=, #nonblock?, #read, #sysread, #syswrite, #wait, wrap, wrap_blocking_method, wraps, #write

Constructor Details

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

Returns a new instance of Line.



29
30
31
32
33
# File 'lib/async/io/protocol/line.rb', line 29

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

Instance Attribute Details

#eolObject (readonly)

Returns the value of attribute eol.



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

def eol
  @eol
end

Instance Method Details

#each_lineObject



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

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)


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

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



50
51
52
# File 'lib/async/io/protocol/line.rb', line 50

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

#read_linesObject



72
73
74
# File 'lib/async/io/protocol/line.rb', line 72

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

#write_lines(*args) ⇒ Object



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

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