Class: IOP::StringMerger
Overview
Note:
instance of this class can be used to collect data from multiple processing runs.
Sink class to receive data blocks and merge them into a single string.
### Use case: read current source file into a string.
require 'iop/file'
require 'iop/string'
( IOP::FileReader.new($0) | (s = IOP::StringMerger.new) ).process!
puts s.to_s
The actual string assembly is performed by the #to_s method.
Instance Attribute Summary
Attributes included from Sink
Instance Method Summary collapse
-
#initialize ⇒ StringMerger
constructor
Creates class instance.
- #process(data = nil) ⇒ Object
-
#to_s ⇒ String
Returns concatenation of all received data blocks into a single string.
Methods included from Sink
Constructor Details
#initialize ⇒ StringMerger
Creates class instance.
65 66 67 68 |
# File 'lib/iop/string.rb', line 65 def initialize @size = 0 @data = [] end |
Instance Method Details
#process(data = nil) ⇒ Object
70 71 72 73 74 75 |
# File 'lib/iop/string.rb', line 70 def process(data = nil) unless data.nil? @data << data.dup # CHECKME is duplication really needed when the upstream continuously resending its internal data buffer with new contents @size += data.size end end |
#to_s ⇒ String
Returns concatenation of all received data blocks into a single string.
80 81 82 83 84 |
# File 'lib/iop/string.rb', line 80 def to_s string = IOP.allocate_string(@size) @data.each {|x| string << x} string end |