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
Source
# 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
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
Public Instance Methods
Source
# File lib/stream.rb 355 def at_beginning? 356 @position_holder.at_beginning? 357 end
Source
# 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
at_end? has to look ahead if there is an element satisfing the filter
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
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
Source
# File lib/stream.rb 403 def pos 404 @position_holder.pos 405 end
Returns the current position of the stream.
Source
# File lib/stream.rb 396 def set_to_begin 397 super 398 @peek = nil 399 @position_holder.set_to_begin 400 end
Calls superclass method
Stream::WrappedStream#set_to_begin
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