Class: ConfCtl::LineBuffer
- Inherits:
-
Object
- Object
- ConfCtl::LineBuffer
- Defined in:
- lib/confctl/line_buffer.rb
Overview
Feed string data and get output as lines
Instance Method Summary collapse
-
#<<(str) ⇒ Object
Feed string.
-
#flush ⇒ String
Return the buffer’s contents and flush it.
-
#initialize {|line| ... } ⇒ LineBuffer
constructor
If instantiated with a block, the block is invoked for each read line.
-
#read_line ⇒ String?
Read one line if there is one.
Constructor Details
#initialize {|line| ... } ⇒ LineBuffer
If instantiated with a block, the block is invoked for each read line
6 7 8 9 |
# File 'lib/confctl/line_buffer.rb', line 6 def initialize(&block) @buffer = '' @block = block end |
Instance Method Details
#<<(str) ⇒ Object
Feed string
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/confctl/line_buffer.rb', line 13 def <<(str) buffer << str return if block.nil? loop do out_line = read_line break if out_line.nil? block.call(out_line) end end |
#flush ⇒ String
Return the buffer’s contents and flush it
If block was given to ConfCtl::LineBuffer, it will be invoked with the buffer contents.
42 43 44 45 46 47 |
# File 'lib/confctl/line_buffer.rb', line 42 def flush ret = buffer.clone buffer.clear block.call(ret) if block ret end |
#read_line ⇒ String?
Read one line if there is one
27 28 29 30 31 32 33 34 |
# File 'lib/confctl/line_buffer.rb', line 27 def read_line nl = buffer.index("\n") return if nl.nil? line = buffer[0..nl] @buffer = buffer[nl + 1..] line end |