Class Enumerable::Enumerator
In: lib/core/facets/to_hash.rb
lib/lore/facets/enumerator.rb
lib/more/facets/elementor.rb
Parent: Object

Methods

new   to_elem   to_h  

External Aliases

initialize -> old_initialize

Public Class methods

Provides the ruby-1.9 block form of Enumerator, where you can write:

   obj = Enumerator.new do |yielder|
      .. do stuff
      yielder.yield data      # or: yielder << data
      .. etc
   end

When obj.each is called, the block is run once. It should call yielder.yield with each item it wishes to generate.

Example:

  fib = Enumerator.new { |y|
    a = b = 1
    loop {
      y << a
      a, b = b, a + b
    }
  }

  assert_equal [1, 1, 2, 3, 5, 8, 13, 21, 34, 55], fib.take(10)

[Source]

# File lib/lore/facets/enumerator.rb, line 34
      def initialize(*args, &block)
        if block_given?
          @body = block
          old_initialize(self, :_start)
        else
          old_initialize(*args)
        end
      end

Public Instance methods

Create Elementor.

[Source]

# File lib/more/facets/elementor.rb, line 124
  def to_elem(meth=nil)
    Elementor.new(self, meth || :each)
  end

Convert an Enumerable::Enumerator object directly into a hash.

  e = [1,2,3,4,5].to_enum
  e.to_h  #=> {5=>nil, 1=>2, 3=>4}
  e2 = [1,2,1,3,1,5].to_enum
  e2.to_h #=> {1=>5}
  e3 = [[1,:a],[2,:b],[3,:c]].to_enum
  e3.to_h #=> { 1=>:a, 2=>:b, 3=>:c }

CREDIT: Sandor Szücs

[Source]

# File lib/core/facets/to_hash.rb, line 304
    def to_h(mode=nil)
      to_a.to_h(mode)
    end

[Validate]