Class: Enumerator

Inherits:
Object show all
Defined in:
lib/backports/1.9.1/enumerator/new.rb,
lib/backports/2.0.0/enumerable/lazy.rb,
lib/backports/2.7.0/enumerator/produce.rb

Overview

new with block, standard in Ruby 1.9

Direct Known Subclasses

Chain, Lazy, Product

Defined Under Namespace

Classes: Chain, GeneratorBP, Lazy, Product, Yielder

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.produce(initial = Backports::Undefined) ⇒ Object

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/backports/2.7.0/enumerator/produce.rb', line 7

def self.produce(initial = Backports::Undefined)
  raise ArgumentError, 'no block given' unless block_given?

  Enumerator.new do |y|
    val = initial == Backports::Undefined ? yield() : initial

    loop do
      y << val
      val = yield(val)
    end
  end
end

.product(*enums, &block) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/backports/3.2.0/enumerator/product.rb', line 12

def Enumerator.product(*enums, &block)
  kwargs = enums[-1]
  if kwargs.is_a?(Hash) && !kwargs.empty?
    raise ArgumentError, "unknown keywords: #{kwargs.keys.map(&:inspect).join(', ')}"
  end
  Enumerator::Product.new(*enums).each(&block)
end

Instance Method Details

#initialize_with_optional_block(*arg, &block) ⇒ Object



34
35
36
37
# File 'lib/backports/1.9.1/enumerator/new.rb', line 34

def initialize_with_optional_block(*arg, &block)
  return initialize_without_optional_block(*arg, &nil) unless arg.empty?  # Ruby 1.9 apparently ignores the block if any argument is present
  initialize_without_optional_block(GeneratorBP.new(&block))
end