Class: IOP::StringSplitter
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!
Instance Attribute Summary
Attributes included from Feed
Instance Method Summary collapse
-
#initialize(string, block_size: DEFAULT_BLOCK_SIZE) ⇒ StringSplitter
constructor
Creates class instance.
- #process! ⇒ Object
Methods included from Feed
Constructor Details
#initialize(string, block_size: DEFAULT_BLOCK_SIZE) ⇒ StringSplitter
Creates class instance.
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
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 |