Class: Enumerator::Chain

Inherits:
Enumerator show all
Defined in:
enumerator.c,
enumerator.c

Overview

Enumerator::Chain is a subclass of Enumerator, which represents a chain of enumerables that works as a single enumerator.

This type of objects can be created by Enumerable#chain and Enumerator#+.

Instance Method Summary collapse

Methods inherited from Enumerator

#+, #each_with_index, #each_with_object, #feed, #next, #next_values, #peek, #peek_values, produce, #with_index, #with_object

Methods included from Enumerable

#all?, #any?, #chain, #chunk, #chunk_while, #collect, #collect_concat, #count, #cycle, #detect, #drop, #drop_while, #each_cons, #each_entry, #each_slice, #each_with_index, #each_with_object, #entries, #filter, #filter_map, #find, #find_all, #find_index, #first, #flat_map, #grep, #grep_v, #group_by, #include?, #inject, #lazy, #map, #max, #max_by, #member?, #min, #min_by, #minmax, #minmax_by, #none?, #one?, #partition, #reduce, #reject, #reverse_each, #select, #slice_after, #slice_before, #slice_when, #sort, #sort_by, #sum, #take, #take_while, #tally, #to_a, #to_h, #uniq, #zip

Constructor Details

#Enumerator::Chain.new(*enums) ⇒ Enumerator

Generates a new enumerator object that iterates over the elements of given enumerable objects in sequence.

e = Enumerator::Chain.new(1..3, [4, 5])
e.to_a #=> [1, 2, 3, 4, 5]
e.size #=> 5


3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
# File 'enumerator.c', line 3091

static VALUE
enum_chain_initialize(VALUE obj, VALUE enums)
{
    struct enum_chain *ptr;

    rb_check_frozen(obj);
    TypedData_Get_Struct(obj, struct enum_chain, &enum_chain_data_type, ptr);

    if (!ptr) rb_raise(rb_eArgError, "unallocated chain");

    ptr->enums = rb_obj_freeze(enums);
    ptr->pos = -1;

    return obj;
}

Instance Method Details

#each(*args) {|...| ... } ⇒ Object #each(*args) ⇒ Object

Iterates over the elements of the first enumerable by calling the “each” method on it with the given arguments, then proceeds to the following enumerables in sequence until all of the enumerables are exhausted.

If no block is given, returns an enumerator.

Overloads:

  • #each(*args) {|...| ... } ⇒ Object

    Yields:

    • (...)

    Returns:



3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
# File 'enumerator.c', line 3187

static VALUE
enum_chain_each(int argc, VALUE *argv, VALUE obj)
{
    VALUE enums, block;
    struct enum_chain *objptr;
    long i;

    RETURN_SIZED_ENUMERATOR(obj, argc, argv, argc > 0 ? enum_chain_enum_no_size : enum_chain_enum_size);

    objptr = enum_chain_ptr(obj);
    enums = objptr->enums;
    block = rb_block_proc();

    for (i = 0; i < RARRAY_LEN(enums); i++) {
        objptr->pos = i;
        rb_funcall_with_block(RARRAY_AREF(enums, i), id_each, argc, argv, block);
    }

    return obj;
}

#initialize_copy(orig) ⇒ Object

:nodoc:



3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
# File 'enumerator.c', line 3108

static VALUE
enum_chain_init_copy(VALUE obj, VALUE orig)
{
    struct enum_chain *ptr0, *ptr1;

    if (!OBJ_INIT_COPY(obj, orig)) return obj;
    ptr0 = enum_chain_ptr(orig);

    TypedData_Get_Struct(obj, struct enum_chain, &enum_chain_data_type, ptr1);

    if (!ptr1) rb_raise(rb_eArgError, "unallocated chain");

    ptr1->enums = ptr0->enums;
    ptr1->pos = ptr0->pos;

    return obj;
}

#inspectString

Returns a printable version of the enumerator chain.

Returns:



3255
3256
3257
3258
3259
# File 'enumerator.c', line 3255

static VALUE
enum_chain_inspect(VALUE obj)
{
    return rb_exec_recursive(inspect_enum_chain, obj, 0);
}

#rewindObject

Rewinds the enumerator chain by calling the “rewind” method on each enumerable in reverse order. Each call is performed only if the enumerable responds to the method.

Returns:



3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
# File 'enumerator.c', line 3216

static VALUE
enum_chain_rewind(VALUE obj)
{
    struct enum_chain *objptr = enum_chain_ptr(obj);
    VALUE enums = objptr->enums;
    long i;

    for (i = objptr->pos; 0 <= i && i < RARRAY_LEN(enums); objptr->pos = --i) {
        rb_check_funcall(RARRAY_AREF(enums, i), id_rewind, 0, 0);
    }

    return obj;
}

#sizeInteger, ...

Returns the total size of the enumerator chain calculated by summing up the size of each enumerable in the chain. If any of the enumerables reports its size as nil or Float::INFINITY, that value is returned as the total size.

Returns:



3157
3158
3159
3160
3161
# File 'enumerator.c', line 3157

static VALUE
enum_chain_size(VALUE obj)
{
    return enum_chain_total_size(enum_chain_ptr(obj)->enums);
}