top | item 9766082

(no title)

indspenceable | 10 years ago

Re: Generators - I don't know why it's so hidden, but the Enumerator class allows you to make generators

```

enum = Enumerator.new{|y| a = 0; loop{y << a; a += 1}}

enum.next => 0

enum.next => 1

etc

```

I believe this has existed since 1.8x

discuss

order

cremno|10 years ago

What makes you think they're hidden? Enumerator moved from stdlib to core in 1.9 and also calling #to_enum isn't needed anymore:

    enum = 0.upto(Float::INFINITY)
    enum.next # => 0
    enum.next # => 1

rylee|10 years ago

It's not "hidden" per se, it's the first SO result for googling "ruby generators".