Class: Stream::FilteredStream

### Home Classes Methods

### In Files

- lib/stream.rb

### Parent

Stream::WrappedStream

### Methods

- ::new
- #at_beginning?
- #at_end?
- #basic_backward
- #basic_forward
- #pos
- #set_to_begin
- #set_to_end

### Class/Module Index

Quicksearch

- Stream
- Stream::BasicStream
- Stream::CollectionStream
- Stream::ConcatenatedStream
- Stream::EmptyStream
- Stream::EndOfStreamException
- Stream::FilteredStream
- Stream::ImplicitStream
- Stream::IntervalStream
- Stream::MappedStream
- Stream::ReversedStream
- Stream::WrappedStream
- Array
- Enumerable
- Generator
- Object

No matching classes.

# 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(otherStream, &filter) click to toggle source

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

    # File lib/stream.rb, line 243
    def initialize (otherStream, &filter)
      super otherStream
      @filter = filter
      @positionHolder = IntervalStream.new
      set_to_begin
    end

### Public Instance Methods

at_beginning?() click to toggle source

    # File lib/stream.rb, line 250
    def at_beginning?; @positionHolder.at_beginning?; 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, line 253
    def at_end?
      @positionHolder.at_end? and
            begin
              if @peek.nil?
                    @peek = wrapped_stream.move_forward_until( &@filter ) or return true
                    @positionHolder.increment_stop
              end
              false
            end
    end

basic_backward() click to toggle source

    # File lib/stream.rb, line 277
    def basic_backward
      wrapped_stream.backward unless @peek.nil?
      @peek = nil
      @positionHolder.backward
      wrapped_stream.move_backward_until(&@filter) or self
    end

basic_forward() click to toggle source

    # File lib/stream.rb, line 264
    def basic_forward
      result =
            if @peek.nil?
              wrapped_stream.move_forward_until(&@filter)
            else
              # Do not move!!
              @peek
            end
      @peek = nil
      @positionHolder.forward
      result
    end

pos() click to toggle source

Returns the current position of the stream.

    # File lib/stream.rb, line 296
    def pos; @positionHolder.pos; end

set_to_begin() click to toggle source

    # File lib/stream.rb, line 289
    def set_to_begin
      super
      @peek = nil
      @positionHolder.set_to_begin
    end

set_to_end() click to toggle source

    # File lib/stream.rb, line 284
    def set_to_end
      # Not super which is a WrappedStream, but same behavior as in Stream
      until at_end?; basic_forward; end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.