class Stream::FilteredStream

A FilteredStream selects all elements which satisfy a given booelan block of another stream being wrapped.

A FilteredStream is created by the method filtered:

(1..6).create_stream.filtered { |x| x % 2 == 0 }.to_a ==> [2, 4, 6]

Public Class Methods

new(other_stream, &filter) click to toggle source

Create a new FilteredStream wrapping other_stream and selecting all its elements which satisfy the condition defined by the block_filter_.

Calls superclass method Stream::WrappedStream::new
    # File lib/stream.rb
348 def initialize(other_stream, &filter)
349   super other_stream
350   @filter = filter
351   @position_holder = IntervalStream.new
352   set_to_begin
353 end

Public Instance Methods

at_beginning?() click to toggle source
    # File lib/stream.rb
355 def at_beginning?
356   @position_holder.at_beginning?
357 end
at_end?() click to toggle source

at_end? has to look ahead if there is an element satisfing the filter

    # File lib/stream.rb
360 def at_end?
361   @position_holder.at_end? and
362       begin
363         if @peek.nil?
364           @peek = wrapped_stream.move_forward_until(&@filter) or return true
365           @position_holder.increment_stop
366         end
367         false
368       end
369 end
basic_backward() click to toggle source
    # File lib/stream.rb
384 def basic_backward
385   wrapped_stream.backward unless @peek.nil?
386   @peek = nil
387   @position_holder.backward
388   wrapped_stream.move_backward_until(&@filter) or self
389 end
basic_forward() click to toggle source
    # File lib/stream.rb
371 def basic_forward
372   result =
373       if @peek.nil?
374         wrapped_stream.move_forward_until(&@filter)
375       else
376         # Do not move!!
377         @peek
378       end
379   @peek = nil
380   @position_holder.forward
381   result
382 end
pos() click to toggle source

Returns the current position of the stream.

    # File lib/stream.rb
403 def pos
404   @position_holder.pos
405 end
set_to_begin() click to toggle source
Calls superclass method Stream::WrappedStream#set_to_begin
    # File lib/stream.rb
396 def set_to_begin
397   super
398   @peek = nil
399   @position_holder.set_to_begin
400 end
set_to_end() click to toggle source
    # File lib/stream.rb
391 def set_to_end
392   # Not super which is a WrappedStream, but same behavior as in Stream
393   basic_forward until at_end?
394 end