Enumerator

Nearly every Enumerable method that takes a block argument can also be called without a block, in which case the method returns an Enumerator. (There are a few subclasses of Enumerator that you wouldn’t create by hand but which implement some specialized logic.) In addition to those Enumerable methods, you can create an enumerator in a few other ways.

Creating Enumerators

Any object can be converted into an Enumerator with the Object#to_enum(method = :each, *args) method (aliased as enum_for). The first argument to to_enum is a symbol that’s the name of a method that converts the object to something enumerable. Any further arguments to to_enum are passed to the method named in the first argument. You can then treat that enumerator ...

Get Programming Ruby 3.3 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.