Class: Enumerator::Chain
- Inherits:
-
Enumerator
- Object
- Enumerator
- Enumerator::Chain
- 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
-
#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.
-
#Enumerator::Chain.new(*enums) ⇒ Enumerator
constructor
Generates a new enumerator object that iterates over the elements of given enumerable objects in sequence.
-
#initialize_copy(orig) ⇒ Object
:nodoc:.
-
#inspect ⇒ String
Returns a printable version of the enumerator chain.
-
#rewind ⇒ Object
Rewinds the enumerator chain by calling the “rewind” method on each enumerable in reverse order.
-
#size ⇒ Integer, ...
Returns the total size of the enumerator chain calculated by summing up the size of each enumerable in the chain.
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
3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 |
# File 'enumerator.c', line 3102
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.
3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 |
# File 'enumerator.c', line 3198
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:
3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 |
# File 'enumerator.c', line 3119
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;
}
|
#inspect ⇒ String
Returns a printable version of the enumerator chain.
3266 3267 3268 3269 3270 |
# File 'enumerator.c', line 3266
static VALUE
enum_chain_inspect(VALUE obj)
{
return rb_exec_recursive(inspect_enum_chain, obj, 0);
}
|
#rewind ⇒ Object
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.
3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 |
# File 'enumerator.c', line 3227
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;
}
|
#size ⇒ Integer, ...
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.
3168 3169 3170 3171 3172 |
# File 'enumerator.c', line 3168
static VALUE
enum_chain_size(VALUE obj)
{
return enum_chain_total_size(enum_chain_ptr(obj)->enums);
}
|