3

As far as I understand the Array class already mixes in the Enumerable module.

If that's so, why isn't there [:example].next?

Why do I need to make it [:example].to_enum.next?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Benedikt B
  • 733
  • 8
  • 23
  • 1
    Because the method `next` is defined on the class `Enumerator` and not `Array`. Array mixes in Enumerable which makes the conversation to `Enumerator` possible in the first place. – Konrad Reiche Sep 27 '13 at 14:50
  • Same as [this](http://stackoverflow.com/questions/2863044/what-is-the-advantage-of-creating-an-enumerable-object-using-to-enum-in-ruby)? – Cary Swoveland Sep 27 '13 at 14:51

2 Answers2

7

to_enum has nothing to do with Enumerable, it returns an Enumerator. Array doesn't have a next method because next is an Enumerator method, not an Enumerable method.

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
3

Because the Enumerable module is different from the Enumerator class.

Being "Enumerable" means that the class gets a bunch of freebie methods that create "Enumerators". Compare to Java's Iterable and Iterator interfaces.

maerics
  • 151,642
  • 46
  • 269
  • 291