3 ms·
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 +
by indspenceable 11y 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
- rylee 11y agoIt's not "hidden" per se, it's the first SO result for googling "ruby generators".
- cremno 11y agoWhat 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