What is An Enumerator in Ruby?

Enumerator in Ruby:

In Ruby, a built-in Enumerator class implements external iterators. You can create an Enumerator object by calling the to [ruby]_enum[/ruby] method on a collection such as an array or a hash:

a = [ 1, 3, "cat" ]
h = { dog: "canine", fox: "vulpine" }

Creating Enumerators:

enum_a = a.to_enum
enum_h = h.to_enum
enum_a.next # => 1
enum_h.next # => [:dog, "canine"]
enum_a.next # => 3
enum_h.next # => [:fox, "vulpine"]

Most of the internal iterator methods—the ones that normally yield successive values to a block, will also return an Enumerator object if called without a block:

a = [ 1, 3, "cat" ]
enum_a = a.each # create an Enumerator using an internal iterator
enum_a.next # => 1
enum_a.next # => 3