class TrickBag::Enumerables::FilteredEnumerable

Decorator that wraps an Enumerable and takes a filter predicate to determine whether or not objects from the wrapped enumerator should be processed.

The filter is an optional parameter on the constructor, but can also be set after creation with a mutator.

If you do not intend to change the filter after it is created, you should probably just use Ruby's built in lazy enumerators, as in: e = (1..10000000000000000000000000000).lazy.select { |n| n.even? }.lazy

This class really comes in handy when the filter needs to be changed during the enumerator's lifetime.

Attributes

filter[RW]
wrapped_enumerator[R]

Public Class Methods

new(wrapped_enumerator, filter = ->(_) { true } click to toggle source

@param wrapped_enumerator the enumerator to filter @param filter a lambda that returns whether or not to yield a given object,

defaults to lambda always returning true
# File lib/trick_bag/enumerables/filtered_enumerable.rb, line 29
def initialize(wrapped_enumerator, filter = ->(_) { true })
  @wrapped_enumerator = wrapped_enumerator
  @filter = filter
end

Public Instance Methods

each() { |thing| ... } click to toggle source

Standard Enumerable.each; returns an Enumerator if called without a block

# File lib/trick_bag/enumerables/filtered_enumerable.rb, line 35
def each
  return to_enum unless block_given?

  wrapped_enumerator.each do |thing|
    yield thing if filter.(thing)
  end
end