Understanding Enumerator's yielder

I found a good (and simple) example of Enumerators (Ruby 1.9) in the
pickaxe book, the triangular_numbers from which I devised a simpler
(although rather useless) example:

m = Enumerator.new { |yielder| loop {yielder.yield 1 } }

I faithfully copied that 'yielder' parameter but to be honest I don't
understand where it is defined. Is it possible to modify it? It looks
like an usual code block which gets called with a parameter.

···

--
Posted via http://www.ruby-forum.com/.

m = Enumerator.new { |yielder| loop {yielder.yield 1 } }

I faithfully copied that 'yielder' parameter but to be honest I don't
understand where it is defined.

It's a parameter to the block.

  Is it possible to modify it?

You mean name it something else? Of course. This works just fine:
Enumerator.new {|chunky_bacon| loop {chunky_bacon.yield 1}}

  It looks
like an usual code block which gets called with a parameter.

It is.

···

On 14.05.2012 19:22, Földes László wrote:

Hi,

Földes László wrote in post #1060708:

I found a good (and simple) example of Enumerators (Ruby 1.9) in the
pickaxe book, the triangular_numbers from which I devised a simpler
(although rather useless) example:

m = Enumerator.new { |yielder| loop {yielder.yield 1 } }

I faithfully copied that 'yielder' parameter but to be honest I don't
understand where it is defined. Is it possible to modify it?

Modify what? The block parameter contains an instance of
Enumerator::Yielder, which is used to pass control around.

···

--
Posted via http://www.ruby-forum.com/\.

Jan E. wrote in post #1060715:

Modify what? The block parameter contains an instance of
Enumerator::Yielder, which is used to pass control around.

Thanks.

···

--
Posted via http://www.ruby-forum.com/\.