```2010/8/2 Brian Candler <b.candler / pobox.com>:
> Benoit Daloze wrote:
>> So, with Enumerator it should be:
>>
>> even =3D Enumerator.new { |y|
>> =A0 i =3D 0
>> =A0 loop {
>> =A0 =A0 y << i
>> =A0 =A0 i +=3D 2
>> =A0 }
>> }
>>
>> p even.take(5) # =3D> [0, 2, 4, 6, 8]
>>
>> But that is not really common practice in Ruby (Enumerator for so
>> simple things).
>
> In any case, that Enumerator block form is only syntactic sugar. You can
> do the same without using the Enumerator class:
>
> =A0even =3D Object.new
> =A0def even.each(&blk)
> =A0 =A0(2..1.0/0).step(2,&blk)
> =A0end
> =A0even.extend Enumerable
> =A0p even.take(5)

Even simpler:

irb(main):014:0> even =3D (2..1.0/0).step(2)
=3D> #<Enumerator:0x1027efc4>
irb(main):015:0> even.take 5
=3D> [2.0, 4.0, 6.0, 8.0, 10.0]

irb(main):016:0> even =3D 2.step(1/0.0, 2)
=3D> #<Enumerator:0x102a070c>
irb(main):017:0> even.take 5
=3D> [2.0, 4.0, 6.0, 8.0, 10.0]

(pun intended)

> In both cases, all you're doing is having 'each' generate an infinite
> series, and then truncating it with 'take'.
>
> This is not useful normally, because you can't chain it - most of the
> Enumerable =A0functions like 'map', 'select' etc collect the full results
> into an Array before continuing.
>
> However, it's quite possible to have these functions process one element
> at a time without generating the intermediate arrays, and there is an
> implementation of this in the 'facets' library:
>
>
>>> RUBY_VERSION
> =3D> "1.8.6"
>>> require 'rubygems'
> =3D> true
>>> require 'facets/enumerator'
> =3D> true
>>> require 'facets/enumerable/defer'
> =3D> true
>>> (1..1_000_000_000).defer.select { |i| i % 2 =3D=3D 0 }.map { |i| i+100 =
}.take(10).to_a
> =3D> [102, 104, 106, 108, 110, 112, 114, 116, 118, 120]
>
>
> Infinite lists are no problem here - each element propagates through the
> whole chain before the next. You're processing 'horizontally' rather
> than 'vertically'.
>
> Note that this doesn't use Threads or Fibers, it's efficient, and it
> works in ruby 1.8. For the implementation details, see the Denumerator
> class.

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

```