Class: Classifier::Streaming::MultiIO
- Defined in:
- lib/classifier/streaming/multi_io.rb
Overview
A utility class that wraps multiple IO-like streams and treats them as a single, sequential stream.
MultiIO allows you to iterate over lines across multiple input sources
(such as files, standard input, or string buffers) seamlessly in the order
they are provided.
If a string is passed as an argument, it is automatically treated as a file path and opened. The file will be safely closed immediately after the iteration is complete.
Instance Method Summary collapse
- #each_line ⇒ Object
-
#initialize(sources) ⇒ MultiIO
constructor
A new instance of MultiIO.
Constructor Details
#initialize(sources) ⇒ MultiIO
Returns a new instance of MultiIO.
30 31 32 |
# File 'lib/classifier/streaming/multi_io.rb', line 30 def initialize(sources) @sources = sources.dup end |
Instance Method Details
#each_line ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/classifier/streaming/multi_io.rb', line 36 def each_line # rubocop:disable Style/ExplicitBlockArgument return enum_for(:each_line) unless block_given? @sources.each do |source| if source.is_a?(String) File.open(source) { |io| io.each_line { |line| yield line } } else source.each_line { |line| yield line } end end # rubocop:enable Style/ExplicitBlockArgument end |