Class: Lazily::Filter

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/lazily/filtering.rb

Instance Method Summary collapse

Methods included from Enumerable

#[], #chunk, #collect, #compact, #concat, #drop, #drop_while, #flat_map, #flatten, #grep, #in_threads, #lazily, #lazy?, #prefetch, #reject, #select, #slice_before, #take, #take_while, #uniq, #zip

Methods included from Enumerable

#lazily

Constructor Details

#initialize(source, method, &generator) ⇒ Filter

Returns a new instance of Filter.



235
236
237
238
239
# File 'lib/lazily/filtering.rb', line 235

def initialize(source, method, &generator)
  @source = source
  @method = method
  @generator = generator
end

Instance Method Details

#eachObject



241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/lazily/filtering.rb', line 241

def each
  return to_enum unless block_given?
  yielder = proc { |x| yield x }
  if @generator.arity == 2
    # we might want to terminate early
    all_done = "Lazily::loop-done-#{object_id}".to_sym
    catch all_done do
      @generator.call(yielder, all_done)
    end
  else
    @generator.call(yielder)
  end
end

#inspectObject



255
256
257
# File 'lib/lazily/filtering.rb', line 255

def inspect
  "#<#{self.class}: #{@method} #{@source.inspect}>"
end