Method: Object#enum_for

Defined in:
enumerator.c

#to_enum(method = :each, *args) ⇒ Object #enum_for(method = :each, *args) ⇒ Object

Returns Enumerator.new(self, method, *args).

e.g.:

str = "xyz"

enum = str.enum_for(:each_byte)
a = enum.map {|b| '%02x' % b } #=> ["78", "79", "7a"]

# protects an array from being modified
a = [1, 2, 3]
some_method(a.to_enum)

# File 'enumerator.c'

/*
 *  call-seq:
 *    obj.to_enum(method = :each, *args)
 *    obj.enum_for(method = :each, *args)
 *
 *  Returns Enumerator.new(self, method, *args).
 *
 *  e.g.:
 *
 *     str = "xyz"
 *
 *     enum = str.enum_for(:each_byte)
 *     a = enum.map {|b| '%02x' % b } #=> ["78", "79", "7a"]
 *
 *     # protects an array from being modified
 *     a = [1, 2, 3]
 *     some_method(a.to_enum)
 *
 */
static VALUE
obj_to_enum(int argc, VALUE *argv, VALUE obj)
{
    VALUE meth = sym_each;

    if (argc > 0) {
    --argc;
    meth = *argv++;
    }
    return rb_enumeratorize(obj, meth, argc, argv);
}