class IOP::StringMerger

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.

@note instance of this class can be used to collect data from multiple processing runs.

@since 0.1

Public Class Methods

new() click to toggle source

Creates class instance.

# File lib/iop/string.rb, line 65
def initialize
  @size = 0
  @data = []
end

Public Instance Methods

process(data = nil) click to toggle source
# 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() click to toggle source

Returns concatenation of all received data blocks into a single string.

@return [String]

# File lib/iop/string.rb, line 80
def to_s
  string = IOP.allocate_string(@size)
  @data.each {|x| string << x}
  string
end