Class: Jinx::ConditionalEnumerator

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Collection
Defined in:
lib/jinx/helpers/conditional_enumerator.rb

Overview

ConditionalEnumerator applies a filter to another Enumerable.

Examples:

ConditionalEnumerator.new([1, 2, 3]) { |i| i < 3 }.to_a #=> [1, 2]

Instance Method Summary collapse

Methods included from Enumerable

#enumerate, #pp_s, #pretty_print, #pretty_print_cycle, #qp, #to_enum, #transitive_closure

Methods included from Collection

#compact, #compact_map, #detect_value, #detect_with_value, #difference, #empty?, #filter, #first, #flatten, #hashify, #intersect, #join, #last, #partial_sort, #partial_sort!, #partial_sort_by, #size, #to_compact_hash, #to_compact_hash_with_index, #to_series, #transform, #union

Constructor Details

#initialize(base, &filter) ⇒ ConditionalEnumerator

Creates a ConditionalEnumerator which wraps the base Enumerator with a conditional filter.



10
11
12
13
# File 'lib/jinx/helpers/conditional_enumerator.rb', line 10

def initialize(base, &filter)
  @base = base
  @filter = filter
end

Instance Method Details

#eachObject

Applies the iterator block to each of this ConditionalEnumerator’s base Enumerable items for which this ConditionalEnumerator’s filter returns true.



17
18
19
# File 'lib/jinx/helpers/conditional_enumerator.rb', line 17

def each
  @base.each { |item| (yield item) if @filter.call(item) }
end