Class: IOP::StringSplitter

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

Overview

Feed class to send arbitrary string in blocks of specified size.

Use case: split the string into 3-byte blocks and reconstruct it.

require 'iop/string'
( IOP::StringSplitter.new('Hello IOP', 3) | IOP::StringMerger.new ).process!

Since:

  • 0.1

Instance Attribute Summary

Attributes included from Feed

#downstream

Instance Method Summary collapse

Methods included from Feed

#process, #|

Constructor Details

#initialize(string, block_size: DEFAULT_BLOCK_SIZE) ⇒ StringSplitter

Creates class instance.

Parameters:

  • string (String)

    string to be sent in blocks

  • block_size (Integer) (defaults to: DEFAULT_BLOCK_SIZE)

    size of block the string is split into

Since:

  • 0.1



26
27
28
29
# File 'lib/iop/string.rb', line 26

def initialize(string, block_size: DEFAULT_BLOCK_SIZE)
  @string = string
  @block_size = block_size
end

Instance Method Details

#process!Object

Since:

  • 0.1



31
32
33
34
35
36
37
38
39
# File 'lib/iop/string.rb', line 31

def process!
  offset = 0
  (0..@string.size / @block_size - 1).each do
    process(@string[offset, @block_size])
    offset += @block_size
  end
  process(offset.zero? ? @string : @string[offset..-1]) unless offset == @string.size
  process
end