Class: IOP::StringMerger

Inherits:
Object
  • Object
show all
Includes:
Sink
Defined in:
lib/iop/string.rb

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.

Since:

  • 0.1

Instance Attribute Summary

Attributes included from Sink

#upstream

Instance Method Summary collapse

Methods included from Sink

#process!

Constructor Details

#initializeStringMerger

Creates class instance.

Since:

  • 0.1



65
66
67
68
# File 'lib/iop/string.rb', line 65

def initialize
  @size = 0
  @data = []
end

Instance Method Details

#process(data = nil) ⇒ Object

Since:

  • 0.1



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_sString

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

Returns:

  • (String)

Since:

  • 0.1



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