class Stream::ImplicitStream

An ImplicitStream is an easy way to create a stream on the fly without defining a subclass of BasicStream. The basic methods required for a stream are defined with blocks:

s = Stream::ImplicitStream.new { |s|
           x = 0
           s.at_end_proc = proc { x == 5 }
           s.forward_proc = proc { x += 1 }
    }

s.to_a ==> [1, 2, 3, 4, 5]

Note that this stream is only partially defined since backward_proc and at_beginning_proc are not defined. It may as well be useful if only moving forward is required by the code fragment.

ImplicitStreams can be based on other streams using the method modify which is for example used in the methods for creating stream wrappers which remove the first or last element of an existing stream (see remove_first and remove_last).

Attributes

at_beginning_proc[W]
at_end_proc[W]
backward_proc[W]
forward_proc[W]
set_to_begin_proc[W]
set_to_end_proc[W]
wrapped_stream[R]

Public Class Methods

new(other_stream = nil) { |self| ... } click to toggle source

Create a new ImplicitStream which might wrap an existing stream other_stream. If other_stream is supplied the blocks for the basic stream methods are initialized with closures that delegate all operations to the wrapped stream.

If a block is given to new, than it is called with the new ImplicitStream stream as parameter letting the client overwriting the default blocks.

    # File lib/stream.rb
620 def initialize(other_stream = nil)
621   # Initialize with defaults
622   @at_beginning_proc = proc { true }
623   @at_end_proc = proc { true }
624 
625   @set_to_begin_proc = proc {}
626   @set_to_end_proc = proc {}
627 
628   if other_stream
629     @wrapped_stream = other_stream
630     @at_beginning_proc = proc { other_stream.at_beginning? }
631     @at_end_proc = proc { other_stream.at_end? }
632     @forward_proc = proc { other_stream.basic_forward }
633     @backward_proc = proc { other_stream.basic_backward }
634     @set_to_end_proc = proc { other_stream.set_to_end }
635     @set_to_begin_proc = proc { other_stream.set_to_begin }
636   end
637   yield self if block_given? # let client overwrite defaults
638 end

Public Instance Methods

at_beginning?() click to toggle source

Returns the value of @at_beginning_proc.

    # File lib/stream.rb
641 def at_beginning?
642   @at_beginning_proc.call
643 end
at_end?() click to toggle source

Returns the value of @at_end_proc.

    # File lib/stream.rb
646 def at_end?
647   @at_end_proc.call
648 end
basic_backward() click to toggle source

Returns the value of @backward_proc_proc.

    # File lib/stream.rb
656 def basic_backward
657   @backward_proc.call
658 end
basic_forward() click to toggle source

Returns the value of @forward_proc.

    # File lib/stream.rb
651 def basic_forward
652   @forward_proc.call
653 end
set_to_begin() click to toggle source

Calls set_to_begin_proc or super if set_to_begin_proc is undefined.

Calls superclass method Stream#set_to_begin
    # File lib/stream.rb
666 def set_to_begin
667   @set_to_begin_proc ? @set_to_begin_proc.call : super
668 end
set_to_end() click to toggle source

Calls set_to_end_proc or super if set_to_end_proc is undefined.

Calls superclass method Stream#set_to_end
    # File lib/stream.rb
661 def set_to_end
662   @set_to_end_proc ? @set_to_end_proc.call : super
663 end