Parent

Files

Enumerator

Public Class Methods

new(*args, &block) click to toggle source

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

obj = Enumerator.new do |yielder|
  # ...
  yielder.yield(data)  # or: yielder << data
  # ...
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
  }
}  

fib.take(10)  #=> [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
# File lib/facets/enumerator.rb, line 37
def initialize(*args, &block)
  if block
    @body = block
    old_initialize(self, :_start)
  else
    old_initialize(*args)
  end
end
Also aliased as: old_initialize

Public Instance Methods

fx() click to toggle source
# File lib/facets/enumerator/fx.rb, line 15
def fx
  Functor.new(&method(:fx_send).to_proc)
end
to_h(mode=nil) click to toggle source

Convert an Enumerator object into a hash. This is equivalent to Array#to_h.

e1 = [[1,:a],[2,:b],[3,:c]].to_enum
e1.to_h #=> { 1=>:a, 2=>:b, 3=>:c }

e2 = [1,2,3,4,5].to_enum
e2.to_h  #=> {5=>nil, 1=>2, 3=>4}

e3 = [1,2,1,3,1,5].to_enum
e3.to_h #=> {1=>5}

CREDIT: Sandor Szücs

# File lib/facets/to_hash.rb, line 289
def to_h(mode=nil)
  to_a.to_h(mode)
end
to_h_assoc() click to toggle source

This is equivalent to Array#to_h_assoc.

# File lib/facets/to_hash.rb, line 313
def to_h_assoc
  to_a.to_h_assoc
end
to_h_auto() click to toggle source

This is equivalent to Array#to_h_auto.

# File lib/facets/to_hash.rb, line 295
def to_h_auto
  to_a.to_h_auto
end
to_h_flat() click to toggle source

This is equivalent to Array#to_h_flat.

# File lib/facets/to_hash.rb, line 307
def to_h_flat
  to_a.to_h_flat
end
to_h_multi() click to toggle source

This is equivalent to Array#to_h_multi.

# File lib/facets/to_hash.rb, line 319
def to_h_multi
  to_a.to_h_multi
end
to_h_splat() click to toggle source

This is equivalent to Array#to_h_splat.

# File lib/facets/to_hash.rb, line 301
def to_h_splat
  to_a.to_h_splat
end

Private Instance Methods

fx_send(op, *a, &b) click to toggle source
# File lib/facets/enumerator/fx.rb, line 22
def fx_send(op, *a, &b)
  map{ |e| e.send(op, *a, &b) }
end
old_initialize(*args, &block) click to toggle source
Alias for: new

[Validate]

Generated with the Darkfish Rdoc Generator 2.