15#include "ruby/internal/config.h"
24#include "internal/class.h"
25#include "internal/enumerator.h"
26#include "internal/error.h"
27#include "internal/hash.h"
28#include "internal/imemo.h"
29#include "internal/numeric.h"
30#include "internal/range.h"
31#include "internal/rational.h"
165static VALUE rb_cLazy;
166static ID id_rewind, id_to_enum, id_each_entry;
167static ID id_next, id_result, id_receiver, id_arguments, id_memo, id_method, id_force;
168static VALUE sym_each, sym_yield;
170static VALUE lazy_use_super_method;
172extern ID ruby_static_id_cause;
174#define id_call idCall
175#define id_cause ruby_static_id_cause
176#define id_each idEach
178#define id_initialize idInitialize
179#define id_size idSize
198RUBY_REFERENCES(enumerator_refs) = {
211static VALUE rb_cGenerator, rb_cYielder, rb_cEnumProducer;
232 lazyenum_proc_func *proc;
233 lazyenum_size_func *size;
234 lazyenum_precheck_func *precheck;
246static VALUE rb_cEnumChain;
253static VALUE rb_cEnumProduct;
264 RUBY_REFS_LIST_PTR(enumerator_refs),
269 0, NULL, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_DECL_MARKING | RUBY_TYPED_EMBEDDABLE
273enumerator_ptr(
VALUE obj)
278 if (!
ptr || UNDEF_P(
ptr->obj)) {
279 rb_raise(rb_eArgError,
"uninitialized enumerator");
285proc_entry_mark_and_move(
void *p)
288 rb_gc_mark_and_move(&
ptr->proc);
289 rb_gc_mark_and_move(&
ptr->memo);
295 proc_entry_mark_and_move,
298 proc_entry_mark_and_move,
300 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE
383 enumerator = rb_enumeratorize_with_size(obj, meth, argc, argv, 0);
391enumerator_allocate(
VALUE klass)
407 rb_check_frozen(enum_obj);
411 rb_raise(rb_eArgError,
"unallocated enumerator");
423 ptr->size_fn = size_fn;
424 ptr->kw_splat = kw_splat;
430convert_to_feasible_size_value(
VALUE obj)
438 else if (RB_FLOAT_TYPE_P(obj) &&
RFLOAT_VALUE(obj) == HUGE_VAL) {
472enumerator_initialize(
int argc,
VALUE *argv,
VALUE obj)
475 VALUE recv = generator_init(generator_allocate(rb_cGenerator), iter);
476 VALUE arg0 = rb_check_arity(argc, 0, 1) ? argv[0] :
Qnil;
477 VALUE size = convert_to_feasible_size_value(arg0);
479 return enumerator_init(obj, recv, sym_each, 0, 0, 0, size,
false);
489 ptr0 = enumerator_ptr(orig);
498 rb_raise(rb_eArgError,
"unallocated enumerator");
502 ptr1->meth = ptr0->meth;
508 ptr1->size_fn = ptr0->size_fn;
519 return rb_enumeratorize_with_size(obj, meth, argc, argv, 0);
523static int lazy_precheck(
VALUE procs);
531 base_class = rb_cLazy;
534 obj = enumerator_init(enumerator_allocate(
rb_cEnumerator), obj, sym_each, 0, 0, 0,
Qnil,
false);
537 return enumerator_init(enumerator_allocate(base_class),
538 obj, meth, argc, argv, size_fn,
Qnil, kw_splat);
544 return rb_enumeratorize_with_size_kw(obj, meth, argc, argv, size_fn,
rb_keyword_given_p());
551 const VALUE *argv = 0;
552 const struct enumerator *e = enumerator_ptr(obj);
555 VALUE args = e->args;
557 argc = RARRAY_LENINT(args);
605enumerator_each(
int argc,
VALUE *argv,
VALUE obj)
612#if SIZEOF_INT < SIZEOF_LONG
628 if (!lazy_precheck(e->procs))
return Qnil;
630 return enumerator_block_call(obj, 0, obj);
636 struct MEMO *memo = (
struct MEMO *)m;
637 VALUE idx = memo->v1;
638 MEMO_V1_SET(memo, rb_int_succ(idx));
647enumerator_size(
VALUE obj);
652 return enumerator_size(obj);
668enumerator_with_index(
int argc,
VALUE *argv,
VALUE obj)
672 rb_check_arity(argc, 0, 1);
675 return enumerator_block_call(obj, enumerator_with_index_i, (
VALUE)MEMO_NEW(memo, 0, 0));
689enumerator_each_with_index(
VALUE obj)
691 return enumerator_with_index(0, NULL, obj);
736 enumerator_block_call(obj, enumerator_with_object_i, memo);
747 rb_fiber_yield(1, &args);
748 if (!UNDEF_P(e->feedvalue)) {
749 feedvalue = e->feedvalue;
765 return rb_fiber_yield(1, &nil);
771 VALUE curr = rb_fiber_current();
783 VALUE exc = e->stop_exc;
784 VALUE result = rb_attr_get(exc, id_result);
785 VALUE mesg = rb_attr_get(exc, idMesg);
793 curr = rb_fiber_current();
795 if (!e->fib || !rb_fiber_alive_p(e->fib)) {
799 vs = rb_fiber_resume(e->fib, 1, &curr);
856enumerator_next_values(
VALUE obj)
861 rb_check_frozen(obj);
863 if (!UNDEF_P(e->lookahead)) {
869 return get_next_values(obj, e);
873ary2sv(
VALUE args,
int dup)
913enumerator_next(
VALUE obj)
915 VALUE vs = enumerator_next_values(obj);
916 return ary2sv(vs, 0);
920enumerator_peek_values(
VALUE obj)
924 rb_check_frozen(obj);
926 if (UNDEF_P(e->lookahead)) {
927 RB_OBJ_WRITE(obj, &e->lookahead, get_next_values(obj, e));
964enumerator_peek_values_m(
VALUE obj)
966 return rb_ary_dup(enumerator_peek_values(obj));
994enumerator_peek(
VALUE obj)
996 VALUE vs = enumerator_peek_values(obj);
997 return ary2sv(vs, 1);
1051 rb_check_frozen(obj);
1053 if (!UNDEF_P(e->feedvalue)) {
1071enumerator_rewind(
VALUE obj)
1075 rb_check_frozen(obj);
1091inspect_enumerator(
VALUE obj,
VALUE dummy,
int recur)
1094 VALUE eobj, str, cname;
1100 if (!e || UNDEF_P(e->obj)) {
1101 return rb_sprintf(
"#<%"PRIsVALUE
": uninitialized>",
rb_class_path(cname));
1105 str = rb_sprintf(
"#<%"PRIsVALUE
": ...>",
rb_class_path(cname));
1112 eobj = generator_ptr(e->obj)->obj;
1118 str = rb_sprintf(
"#<%"PRIsVALUE
": %+"PRIsVALUE
">",
rb_class_path(cname), eobj);
1121 str = rb_sprintf(
"#<%"PRIsVALUE
": %"PRIsVALUE, cname, str);
1122 append_method(
RARRAY_AREF(e->procs, i), str, e->meth, e->args);
1128 eobj = rb_attr_get(obj, id_receiver);
1134 str = rb_sprintf(
"#<%"PRIsVALUE
": %+"PRIsVALUE,
rb_class_path(cname), eobj);
1135 append_method(obj, str, e->meth, e->args);
1145 if (
SYMBOL_P(key))
return ST_CONTINUE;
1146 *(
int *)arg = FALSE;
1154 rb_str_catf(str,
"% "PRIsVALUE
": %"PRIsVALUE
", ", key, val);
1161 VALUE method, eargs;
1163 method = rb_attr_get(obj, id_method);
1165 if (!
NIL_P(method)) {
1170 method = rb_id2str(default_method);
1176 eargs = rb_attr_get(obj, id_arguments);
1178 eargs = default_args;
1191 rb_hash_foreach(argv[argc-1], key_symbol_p, (
VALUE)&all_key);
1192 if (all_key) kwds = argv[--argc];
1196 VALUE arg = *argv++;
1202 rb_hash_foreach(kwds, kwd_append, str);
1221enumerator_inspect(
VALUE obj)
1256enumerator_size(
VALUE obj)
1260 const VALUE *argv = NULL;
1264 struct generator *g = generator_ptr(e->obj);
1270 struct proc_entry *entry = proc_entry_ptr(proc);
1271 lazyenum_size_func *size_fn = entry->fn->size;
1275 receiver = (*size_fn)(proc, receiver);
1281 return (*e->size_fn)(e->obj, e->args, obj);
1288 if (!UNDEF_P(size))
return size;
1296yielder_mark_and_move(
void *p)
1299 rb_gc_mark_and_move(&
ptr->proc);
1305 yielder_mark_and_move,
1308 yielder_mark_and_move,
1310 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE
1314yielder_ptr(
VALUE obj)
1319 if (!
ptr || UNDEF_P(
ptr->proc)) {
1320 rb_raise(rb_eArgError,
"uninitialized yielder");
1327yielder_allocate(
VALUE klass)
1346 rb_raise(rb_eArgError,
"unallocated yielder");
1356yielder_initialize(
VALUE obj)
1396yielder_to_proc(
VALUE obj)
1412 return yielder_init(yielder_allocate(rb_cYielder),
rb_proc_new(yielder_yield_i, 0));
1419generator_mark_and_move(
void *p)
1422 rb_gc_mark_and_move(&
ptr->proc);
1423 rb_gc_mark_and_move(&
ptr->obj);
1429 generator_mark_and_move,
1432 generator_mark_and_move,
1434 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE
1438generator_ptr(
VALUE obj)
1443 if (!
ptr || UNDEF_P(
ptr->proc)) {
1444 rb_raise(rb_eArgError,
"uninitialized generator");
1451generator_allocate(
VALUE klass)
1467 rb_check_frozen(obj);
1471 rb_raise(rb_eArgError,
"unallocated generator");
1481generator_initialize(
int argc,
VALUE *argv,
VALUE obj)
1495 "wrong argument type %"PRIsVALUE
" (expected Proc)",
1499 rb_warn(
"given block not used");
1503 return generator_init(obj, proc);
1514 ptr0 = generator_ptr(orig);
1519 rb_raise(rb_eArgError,
"unallocated generator");
1529generator_each(
int argc,
VALUE *argv,
VALUE obj)
1544enum_size(
VALUE self)
1547 return UNDEF_P(r) ?
Qnil : r;
1553 return enum_size(self);
1556#define lazy_receiver_size lazy_map_size
1587 rb_block_call(m, id_each, argc-1, argv+1, lazy_init_iterator, val);
1591#define memo_value v2
1592#define memo_flags u3.state
1593#define LAZY_MEMO_BREAK 1
1594#define LAZY_MEMO_PACKED 2
1595#define LAZY_MEMO_BREAK_P(memo) ((memo)->memo_flags & LAZY_MEMO_BREAK)
1596#define LAZY_MEMO_PACKED_P(memo) ((memo)->memo_flags & LAZY_MEMO_PACKED)
1597#define LAZY_MEMO_SET_BREAK(memo) ((memo)->memo_flags |= LAZY_MEMO_BREAK)
1598#define LAZY_MEMO_RESET_BREAK(memo) ((memo)->memo_flags &= ~LAZY_MEMO_BREAK)
1599#define LAZY_MEMO_SET_VALUE(memo, value) MEMO_V2_SET(memo, value)
1600#define LAZY_MEMO_SET_PACKED(memo) ((memo)->memo_flags |= LAZY_MEMO_PACKED)
1601#define LAZY_MEMO_RESET_PACKED(memo) ((memo)->memo_flags &= ~LAZY_MEMO_PACKED)
1603#define LAZY_NEED_BLOCK(func) \
1604 if (!rb_block_given_p()) { \
1605 rb_raise(rb_eArgError, "tried to call lazy " #func " without a block"); \
1616 struct MEMO *result;
1618 result = MEMO_NEW(m, rb_enum_values_pack(argc, argv),
1619 argc > 1 ? LAZY_MEMO_PACKED : 0);
1620 return lazy_yielder_result(result,
yielder, procs_array, memos, 0);
1624lazy_yielder_yield(
struct MEMO *result,
long memo_index,
int argc,
const VALUE *argv)
1626 VALUE m = result->v1;
1630 LAZY_MEMO_SET_VALUE(result, rb_enum_values_pack(argc, argv));
1632 LAZY_MEMO_SET_PACKED(result);
1634 LAZY_MEMO_RESET_PACKED(result);
1635 return lazy_yielder_result(result,
yielder, procs_array, memos, memo_index);
1645 struct proc_entry *entry = proc_entry_ptr(proc);
1646 if (!(*entry->fn->proc)(proc, result, memos, i)) {
1655 if (LAZY_MEMO_BREAK_P(result)) {
1658 return result->memo_value;
1681 struct generator *old_gen_ptr = generator_ptr(e->obj);
1682 obj = old_gen_ptr->obj;
1688 generator = generator_allocate(rb_cGenerator);
1700lazy_precheck(
VALUE procs)
1703 long num_procs =
RARRAY_LEN(procs), i = num_procs;
1706 struct proc_entry *entry = proc_entry_ptr(proc);
1707 lazyenum_precheck_func *precheck = entry->fn->precheck;
1708 if (precheck && !precheck(proc))
return FALSE;
1805lazy_initialize(
int argc,
VALUE *argv,
VALUE self)
1810 rb_check_arity(argc, 1, 2);
1811 LAZY_NEED_BLOCK(
new);
1816 generator = generator_allocate(rb_cGenerator);
1818 enumerator_init(self,
generator, sym_each, 0, 0, 0, size, 0);
1834lazy_to_a(
VALUE self)
1842 ID id = rb_frame_this_func();
1858 lazy_set_args(lazy, args);
1859 e->size_fn = size_fn;
1870 VALUE new_generator;
1875 &proc_entry_data_type, entry);
1882 lazy_set_args(entry_obj, memo);
1885 new_generator = lazy_generator_init(obj, new_procs);
1888 new_obj = enumerator_init_copy(enumerator_allocate(rb_cLazy), obj);
1889 new_e = RTYPEDDATA_GET_DATA(new_obj);
1898 new_e->meth = id_each;
1936enumerable_lazy(
VALUE obj)
1947 return enumerator_init(enumerator_allocate(rb_cLazy),
1948 obj, meth, argc, argv, size_fn,
Qnil, kw_splat);
1975lazy_to_enum(
int argc,
VALUE *argv,
VALUE self)
1977 VALUE lazy, meth = sym_each, super_meth;
1983 if (
RTEST((super_meth = rb_hash_aref(lazy_use_super_method, meth)))) {
1996 return enum_size(self);
2007lazy_eager(
VALUE self)
2010 self, sym_each, 0, 0, lazy_eager_size,
Qnil, 0);
2025 const VALUE *argv = &result->memo_value;
2026 if (LAZY_MEMO_PACKED_P(result)) {
2027 const VALUE args = *argv;
2028 argc = RARRAY_LENINT(args);
2038 LAZY_MEMO_SET_VALUE(result, value);
2039 LAZY_MEMO_RESET_PACKED(result);
2050 lazy_map_proc, lazy_map_size,
2069 LAZY_NEED_BLOCK(map);
2070 return lazy_add_method(obj, 0, 0,
Qnil,
Qnil, &lazy_map_funcs);
2076 struct MEMO *arg = MEMO_CAST(y);
2078 return lazy_yielder_yield(MEMO_CAST(arg->v1), arg->u3.cnt, argc, argv);
2086 const long proc_index = memo_index + 1;
2087 int break_p = LAZY_MEMO_BREAK_P(result);
2089 if (RB_TYPE_P(value,
T_ARRAY)) {
2094 LAZY_MEMO_RESET_BREAK(result);
2096 if (break_p) LAZY_MEMO_SET_BREAK(result);
2103 LAZY_MEMO_RESET_BREAK(result);
2106 lazy_yielder_yield(result, proc_index, 1, &argv);
2108 if (break_p) LAZY_MEMO_SET_BREAK(result);
2112 LAZY_MEMO_SET_VALUE(result, value);
2113 LAZY_MEMO_RESET_PACKED(result);
2118 lazy_flat_map_proc, 0,
2145lazy_flat_map(
VALUE obj)
2147 LAZY_NEED_BLOCK(flat_map);
2148 return lazy_add_method(obj, 0, 0,
Qnil,
Qnil, &lazy_flat_map_funcs);
2155 if (!
RTEST(chain))
return 0;
2160 lazy_select_proc, 0,
2172lazy_select(
VALUE obj)
2174 LAZY_NEED_BLOCK(select);
2175 return lazy_add_method(obj, 0, 0,
Qnil,
Qnil, &lazy_select_funcs);
2182 if (!
RTEST(value))
return 0;
2183 LAZY_MEMO_SET_VALUE(result, value);
2184 LAZY_MEMO_RESET_PACKED(result);
2189 lazy_filter_map_proc, 0,
2203lazy_filter_map(
VALUE obj)
2205 LAZY_NEED_BLOCK(filter_map);
2206 return lazy_add_method(obj, 0, 0,
Qnil,
Qnil, &lazy_filter_map_funcs);
2213 if (
RTEST(chain))
return 0;
2218 lazy_reject_proc, 0,
2229lazy_reject(
VALUE obj)
2231 LAZY_NEED_BLOCK(reject);
2232 return lazy_add_method(obj, 0, 0,
Qnil,
Qnil, &lazy_reject_funcs);
2240 if (!
RTEST(chain))
return 0;
2248 VALUE value, chain =
rb_funcall(entry->memo, id_eqq, 1, result->memo_value);
2250 if (!
RTEST(chain))
return 0;
2254 LAZY_MEMO_SET_VALUE(result, value);
2255 LAZY_MEMO_RESET_PACKED(result);
2261 lazy_grep_iter_proc, 0,
2280 &lazy_grep_iter_funcs : &lazy_grep_funcs;
2281 return lazy_add_method(obj, 0, 0, pattern,
rb_ary_new3(1, pattern), funcs);
2289 if (
RTEST(chain))
return 0;
2297 VALUE value, chain =
rb_funcall(entry->memo, id_eqq, 1, result->memo_value);
2299 if (
RTEST(chain))
return 0;
2302 LAZY_MEMO_SET_VALUE(result, value);
2303 LAZY_MEMO_RESET_PACKED(result);
2309 lazy_grep_v_iter_proc, 0,
2313 lazy_grep_v_proc, 0,
2328 &lazy_grep_v_iter_funcs : &lazy_grep_v_funcs;
2329 return lazy_add_method(obj, 0, 0, pattern,
rb_ary_new3(1, pattern), funcs);
2348 VALUE ary, arrays = entry->memo;
2357 LAZY_MEMO_SET_VALUE(result, ary);
2367 VALUE zip_args = entry->memo;
2386 LAZY_MEMO_SET_VALUE(result, ary);
2391 {lazy_zip_func, lazy_receiver_size,},
2392 {lazy_zip_arrays_func, lazy_receiver_size,},
2415 for (i = 0; i < argc; i++) {
2418 for (; i < argc; i++) {
2420 rb_raise(
rb_eTypeError,
"wrong argument type %"PRIsVALUE
" (must respond to :each)",
2425 funcs = &lazy_zip_funcs[0];
2431 return lazy_add_method(obj, 0, 0, ary, ary, funcs);
2446 if (--remain == 0) LAZY_MEMO_SET_BREAK(result);
2464 return entry->memo !=
INT2FIX(0);
2468 lazy_take_proc, lazy_take_size, lazy_take_precheck,
2484 rb_raise(rb_eArgError,
"attempt to take negative size");
2489 return lazy_add_method(obj, 0, 0, n,
rb_ary_new3(1, n), &lazy_take_funcs);
2497 LAZY_MEMO_SET_BREAK(result);
2504 lazy_take_while_proc, 0,
2515lazy_take_while(
VALUE obj)
2517 LAZY_NEED_BLOCK(take_while);
2518 return lazy_add_method(obj, 0, 0,
Qnil,
Qnil, &lazy_take_while_funcs);
2525 if (
NIL_P(receiver))
2555 lazy_drop_proc, lazy_drop_size,
2574 rb_raise(rb_eArgError,
"attempt to drop negative size");
2577 return lazy_add_method(obj, 2, argv, n,
rb_ary_new3(1, n), &lazy_drop_funcs);
2592 if (
RTEST(drop))
return 0;
2599 lazy_drop_while_proc, 0,
2610lazy_drop_while(
VALUE obj)
2612 LAZY_NEED_BLOCK(drop_while);
2613 return lazy_add_method(obj, 0, 0,
Qfalse,
Qnil, &lazy_drop_while_funcs);
2617lazy_uniq_check(
VALUE chain,
VALUE memos,
long memo_index)
2626 return rb_hash_add_new_element(hash, chain,
Qfalse);
2632 if (lazy_uniq_check(result->memo_value, memos, memo_index))
return 0;
2641 if (lazy_uniq_check(chain, memos, memo_index))
return 0;
2646 lazy_uniq_iter_proc, 0,
2666 return lazy_add_method(obj, 0, 0,
Qnil,
Qnil, funcs);
2672 if (
NIL_P(result->memo_value))
return 0;
2677 lazy_compact_proc, 0,
2688lazy_compact(
VALUE obj)
2690 return lazy_add_method(obj, 0, 0,
Qnil,
Qnil, &lazy_compact_funcs);
2704 argv[0] = result->memo_value;
2708 LAZY_MEMO_RESET_PACKED(result);
2712 LAZY_MEMO_SET_PACKED(result);
2719lazy_with_index_size(
VALUE proc,
VALUE receiver)
2725 lazy_with_index_proc, lazy_with_index_size,
2732 &lazy_with_index_funcs);
2745lazy_each_with_index(
VALUE obj)
2750 return lazy_with_index_from(obj,
LONG2NUM(0));
2771lazy_with_index(
int argc,
VALUE *argv,
VALUE obj)
2779 return lazy_with_index_from(obj, memo);
2791lazy_chunk(
VALUE self)
2802lazy_chunk_while(
VALUE self)
2814lazy_slice_after(
VALUE self)
2826lazy_slice_before(
VALUE self)
2837lazy_slice_when(
VALUE self)
2909stop_result(
VALUE self)
2911 return rb_attr_get(self, id_result);
2919producer_mark_and_move(
void *p)
2922 rb_gc_mark_and_move(&
ptr->init);
2923 rb_gc_mark_and_move(&
ptr->proc);
2924 rb_gc_mark_and_move(&
ptr->size);
2927#define producer_free RUBY_TYPED_DEFAULT_FREE
2930producer_memsize(
const void *p)
2938 producer_mark_and_move,
2941 producer_mark_and_move,
2943 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE
2947producer_ptr(
VALUE obj)
2952 if (!
ptr || UNDEF_P(
ptr->proc)) {
2953 rb_raise(rb_eArgError,
"uninitialized producer");
2960producer_allocate(
VALUE klass)
2981 rb_raise(rb_eArgError,
"unallocated producer");
2994 return rb_attr_get(exc, id_result);
2997NORETURN(
static VALUE producer_each_i(
VALUE obj));
3000producer_each_i(
VALUE obj)
3003 VALUE init, proc, curr;
3005 ptr = producer_ptr(obj);
3009 if (UNDEF_P(init)) {
3027producer_each(
VALUE obj)
3106enumerator_s_produce(
int argc,
VALUE *argv,
VALUE klass)
3113 keyword_ids[0] = rb_intern(
"size");
3117 size = UNDEF_P(size) ?
DBL2NUM(HUGE_VAL) : convert_to_feasible_size_value(size);
3119 if (argc == 0 || (argc == 1 && !
NIL_P(opts))) {
3139enum_chain_mark_and_move(
void *p)
3142 rb_gc_mark_and_move(&
ptr->enums);
3145#define enum_chain_free RUBY_TYPED_DEFAULT_FREE
3148enum_chain_memsize(
const void *p)
3156 enum_chain_mark_and_move,
3159 enum_chain_mark_and_move,
3161 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
3165enum_chain_ptr(
VALUE obj)
3170 if (!
ptr || UNDEF_P(
ptr->enums)) {
3171 rb_raise(rb_eArgError,
"uninitialized chain");
3178enum_chain_allocate(
VALUE klass)
3206 rb_check_frozen(obj);
3209 if (!
ptr) rb_raise(rb_eArgError,
"unallocated chain");
3218new_enum_chain(
VALUE enums)
3221 VALUE obj = enum_chain_initialize(enum_chain_allocate(rb_cEnumChain), enums);
3225 return enumerable_lazy(obj);
3239 ptr0 = enum_chain_ptr(orig);
3243 if (!ptr1) rb_raise(rb_eArgError,
"unallocated chain");
3246 ptr1->pos = ptr0->pos;
3252enum_chain_total_size(
VALUE enums)
3260 if (
NIL_P(size) || (RB_FLOAT_TYPE_P(size) && isinf(
NUM2DBL(size)))) {
3283enum_chain_size(
VALUE obj)
3285 return enum_chain_total_size(enum_chain_ptr(obj)->enums);
3291 return enum_chain_size(obj);
3313enum_chain_each(
int argc,
VALUE *argv,
VALUE obj)
3321 objptr = enum_chain_ptr(obj);
3322 enums = objptr->enums;
3342enum_chain_rewind(
VALUE obj)
3344 struct enum_chain *objptr = enum_chain_ptr(obj);
3345 VALUE enums = objptr->enums;
3348 for (i = objptr->pos; 0 <= i && i <
RARRAY_LEN(enums); objptr->pos = --i) {
3356inspect_enum_chain(
VALUE obj,
VALUE dummy,
int recur)
3363 if (!
ptr || UNDEF_P(
ptr->enums)) {
3364 return rb_sprintf(
"#<%"PRIsVALUE
": uninitialized>",
rb_class_path(klass));
3368 return rb_sprintf(
"#<%"PRIsVALUE
": ...>",
rb_class_path(klass));
3371 return rb_sprintf(
"#<%"PRIsVALUE
": %+"PRIsVALUE
">",
rb_class_path(klass),
ptr->enums);
3381enum_chain_inspect(
VALUE obj)
3401 return new_enum_chain(enums);
3417 return new_enum_chain(rb_ary_new_from_args(2, obj, eobj));
3454enum_product_mark_and_move(
void *p)
3457 rb_gc_mark_and_move(&
ptr->enums);
3460#define enum_product_free RUBY_TYPED_DEFAULT_FREE
3463enum_product_memsize(
const void *p)
3471 enum_product_mark_and_move,
3473 enum_product_memsize,
3474 enum_product_mark_and_move,
3476 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
3480enum_product_ptr(
VALUE obj)
3485 if (!
ptr || UNDEF_P(
ptr->enums)) {
3486 rb_raise(rb_eArgError,
"uninitialized product");
3493enum_product_allocate(
VALUE klass)
3516enum_product_initialize(
int argc,
VALUE *argv,
VALUE obj)
3524 rb_exc_raise(rb_keyword_error_new(
"unknown", rb_hash_keys(options)));
3527 rb_check_frozen(obj);
3530 if (!
ptr) rb_raise(rb_eArgError,
"unallocated product");
3544 ptr0 = enum_product_ptr(orig);
3548 if (!ptr1) rb_raise(rb_eArgError,
"unallocated product");
3556enum_product_total_size(
VALUE enums)
3596enum_product_size(
VALUE obj)
3598 return enum_product_total_size(enum_product_ptr(obj)->enums);
3604 return enum_product_size(obj);
3621 pstate->argv[pstate->index++] = value;
3623 VALUE val = product_each(pstate->obj, pstate);
3634 if (pstate->index < pstate->argc) {
3650 int argc = RARRAY_LENINT(
ptr->enums);
3665 VALUE ret = product_each(obj, &state);
3683enum_product_each(
VALUE obj)
3699enum_product_rewind(
VALUE obj)
3713inspect_enum_product(
VALUE obj,
VALUE dummy,
int recur)
3720 if (!
ptr || UNDEF_P(
ptr->enums)) {
3721 return rb_sprintf(
"#<%"PRIsVALUE
": uninitialized>",
rb_class_path(klass));
3725 return rb_sprintf(
"#<%"PRIsVALUE
": ...>",
rb_class_path(klass));
3728 return rb_sprintf(
"#<%"PRIsVALUE
": %+"PRIsVALUE
">",
rb_class_path(klass),
ptr->enums);
3738enum_product_inspect(
VALUE obj)
3760enumerator_s_product(
int argc,
VALUE *argv,
VALUE klass)
3764 rb_scan_args(argc, argv,
"*:&", &enums, &options, &block);
3767 rb_exc_raise(rb_keyword_error_new(
"unknown", rb_hash_keys(options)));
3770 VALUE obj = enum_product_initialize(argc, argv, enum_product_allocate(rb_cEnumProduct));
3772 if (!
NIL_P(block)) {
3773 enum_product_run(obj, block);
3781 struct enumerator enumerator;
3788RUBY_REFERENCES(arith_seq_refs) = {
3806 "arithmetic_sequence",
3808 RUBY_REFS_LIST_PTR(arith_seq_refs),
3813 .parent = &enumerator_data_type,
3814 .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_DECL_MARKING | RUBY_TYPED_EMBEDDABLE
3818arith_seq_allocate(
VALUE klass)
3846 VALUE aseq = enumerator_init(arith_seq_allocate(rb_cArithSeq),
3854 ptr->exclude_end = excl;
3866arith_seq_begin(
VALUE self)
3879arith_seq_end(
VALUE self)
3893arith_seq_step(
VALUE self)
3906arith_seq_exclude_end(
VALUE self)
3910 return RBOOL(
ptr->exclude_end);
3914arith_seq_exclude_end_p(
VALUE self)
3918 return ptr->exclude_end;
3925 component->
begin = arith_seq_begin(obj);
3926 component->
end = arith_seq_end(obj);
3927 component->
step = arith_seq_step(obj);
3928 component->
exclude_end = arith_seq_exclude_end_p(obj);
3940rb_arithmetic_sequence_beg_len_step(
VALUE obj,
long *begp,
long *lenp,
long *stepp,
long len,
int err)
3942 RBIMPL_NONNULL_ARG(begp);
3943 RBIMPL_NONNULL_ARG(lenp);
3944 RBIMPL_NONNULL_ARG(stepp);
3947 if (!rb_arithmetic_sequence_extract(obj, &aseq)) {
3967 if (err == 0 && (step < -1 || step > 1)) {
3994 rb_raise(rb_eArgError,
"attempt to take negative size");
4000 b = arith_seq_begin(self);
4001 e = arith_seq_end(self);
4002 s = arith_seq_step(self);
4003 x = arith_seq_exclude_end_p(self);
4017 b = rb_big_plus(b, s);
4035 while (n > 0 && i < end) {
4037 if (i > LONG_MAX - unit)
break;
4048 while (n > 0 && i > end) {
4050 if (i < LONG_MIN - unit)
break;
4057 else if (RB_FLOAT_TYPE_P(b) || RB_FLOAT_TYPE_P(e) || RB_FLOAT_TYPE_P(s)) {
4062 double end =
NIL_P(e) ? (unit < 0 ? -1 : 1)*HUGE_VAL :
NUM2DBL(e);
4063 double len = ruby_float_step_size(beg, end, unit, x);
4078 else if (unit == 0) {
4081 for (i = 0; i <
len; ++i) {
4087 for (i = 0; i < n; ++i) {
4088 double d = i*unit+beg;
4089 if (unit >= 0 ? end < d : d < end) d = end;
4113arith_seq_first(
int argc,
VALUE *argv,
VALUE self)
4117 rb_check_arity(argc, 0, 1);
4119 b = arith_seq_begin(self);
4120 e = arith_seq_end(self);
4121 s = arith_seq_step(self);
4139 return arith_seq_take(self, argv[0]);
4146 return rb_int_plus(a, b);
4148 else if (RB_FLOAT_TYPE_P(a)) {
4149 return rb_float_plus(a, b);
4152 return rb_rational_plus(a, b);
4155 return rb_funcallv(a,
'+', 1, &b);
4163 return rb_int_minus(a, b);
4165 else if (RB_FLOAT_TYPE_P(a)) {
4166 return rb_float_minus(a, b);
4169 return rb_rational_minus(a, b);
4172 return rb_funcallv(a,
'-', 1, &b);
4180 return rb_int_mul(a, b);
4182 else if (RB_FLOAT_TYPE_P(a)) {
4183 return rb_float_mul(a, b);
4186 return rb_rational_mul(a, b);
4189 return rb_funcallv(a,
'*', 1, &b);
4198 q = rb_int_idiv(a, b);
4200 else if (RB_FLOAT_TYPE_P(a)) {
4201 q = rb_float_div(a, b);
4204 q = rb_rational_div(a, b);
4207 q = rb_funcallv(a, idDiv, 1, &b);
4213 else if (RB_FLOAT_TYPE_P(q)) {
4214 return rb_float_floor(q, 0);
4217 return rb_rational_floor(q, 0);
4233arith_seq_last(
int argc,
VALUE *argv,
VALUE self)
4235 VALUE b, e, s, len_1,
len, last, nv, ary;
4236 int last_is_adjusted;
4239 e = arith_seq_end(self);
4242 "cannot get the last element of endless arithmetic sequence");
4245 b = arith_seq_begin(self);
4246 s = arith_seq_step(self);
4248 len_1 = num_idiv(num_minus(e, b), s);
4249 if (rb_num_negative_int_p(len_1)) {
4256 last = num_plus(b, num_mul(s, len_1));
4257 if ((last_is_adjusted = arith_seq_exclude_end_p(self) &&
rb_equal(last, e))) {
4258 last = num_minus(last, s);
4265 if (last_is_adjusted) {
4281 rb_raise(rb_eArgError,
"negative array size");
4285 b = rb_int_minus(last, rb_int_mul(s, nv));
4287 b = rb_int_plus(b, s);
4302arith_seq_inspect(
VALUE self)
4305 VALUE eobj, str, eargs;
4310 eobj = rb_attr_get(self, id_receiver);
4316 str = rb_sprintf(
"(%s%"PRIsVALUE
"%s.", range_p ?
"(" :
"", eobj, range_p ?
")" :
"");
4320 eargs = rb_attr_get(eobj, id_arguments);
4333 if (RB_TYPE_P(argv[argc-1],
T_HASH)) {
4335 rb_hash_foreach(argv[argc-1], key_symbol_p, (
VALUE)&all_key);
4336 if (all_key) kwds = argv[--argc];
4340 VALUE arg = *argv++;
4346 rb_hash_foreach(kwds, kwd_append, str);
4373 if (!
rb_equal(arith_seq_begin(self), arith_seq_begin(other))) {
4377 if (!
rb_equal(arith_seq_end(self), arith_seq_end(other))) {
4381 if (!
rb_equal(arith_seq_step(self), arith_seq_step(other))) {
4385 if (arith_seq_exclude_end_p(self) != arith_seq_exclude_end_p(other)) {
4403arith_seq_hash(
VALUE self)
4409 v = rb_hash(arith_seq_begin(self));
4411 v = rb_hash(arith_seq_end(self));
4413 v = rb_hash(arith_seq_step(self));
4420#define NUM_GE(x, y) RTEST(rb_num_coerce_relop((x), (y), idGE))
4435arith_seq_each(
VALUE self)
4437 VALUE c, e, s, len_1, last;
4442 c = arith_seq_begin(self);
4443 e = arith_seq_end(self);
4444 s = arith_seq_step(self);
4445 x = arith_seq_exclude_end_p(self);
4447 if (!RB_TYPE_P(s,
T_COMPLEX) && ruby_float_step(c, e, s, x, TRUE)) {
4454 c = rb_int_plus(c, s);
4468 len_1 = num_idiv(num_minus(e, c), s);
4469 last = num_plus(c, num_mul(s, len_1));
4471 last = num_minus(last, s);
4474 if (rb_num_negative_int_p(s)) {
4475 while (NUM_GE(c, last)) {
4481 while (NUM_GE(last, c)) {
4498arith_seq_size(
VALUE self)
4503 b = arith_seq_begin(self);
4504 e = arith_seq_end(self);
4505 s = arith_seq_step(self);
4506 x = arith_seq_exclude_end_p(self);
4508 if (RB_FLOAT_TYPE_P(b) || RB_FLOAT_TYPE_P(e) || RB_FLOAT_TYPE_P(s)) {
4512 if (rb_num_negative_int_p(s)) {
4524 if (isinf(n))
return DBL2NUM(n);
4526 return rb_dbl2big(n);
4541 len_1 = rb_int_idiv(rb_int_minus(e, b), s);
4542 if (rb_num_negative_int_p(len_1)) {
4546 last = rb_int_plus(b, rb_int_mul(s, len_1));
4557#define sym(name) ID2SYM(rb_intern_const(name))
4559InitVM_Enumerator(
void)
4561 ID id_private = rb_intern_const(
"private");
4595 rb_define_alias(rb_cLazy,
"_enumerable_collect_concat",
"collect_concat");
4610 rb_define_alias(rb_cLazy,
"_enumerable_each_with_index",
"each_with_index");
4612 rb_funcall(rb_cLazy, id_private, 1, sym(
"_enumerable_map"));
4613 rb_funcall(rb_cLazy, id_private, 1, sym(
"_enumerable_collect"));
4614 rb_funcall(rb_cLazy, id_private, 1, sym(
"_enumerable_flat_map"));
4615 rb_funcall(rb_cLazy, id_private, 1, sym(
"_enumerable_collect_concat"));
4616 rb_funcall(rb_cLazy, id_private, 1, sym(
"_enumerable_select"));
4617 rb_funcall(rb_cLazy, id_private, 1, sym(
"_enumerable_find_all"));
4618 rb_funcall(rb_cLazy, id_private, 1, sym(
"_enumerable_filter"));
4619 rb_funcall(rb_cLazy, id_private, 1, sym(
"_enumerable_filter_map"));
4620 rb_funcall(rb_cLazy, id_private, 1, sym(
"_enumerable_reject"));
4621 rb_funcall(rb_cLazy, id_private, 1, sym(
"_enumerable_grep"));
4622 rb_funcall(rb_cLazy, id_private, 1, sym(
"_enumerable_grep_v"));
4623 rb_funcall(rb_cLazy, id_private, 1, sym(
"_enumerable_zip"));
4624 rb_funcall(rb_cLazy, id_private, 1, sym(
"_enumerable_take"));
4625 rb_funcall(rb_cLazy, id_private, 1, sym(
"_enumerable_take_while"));
4626 rb_funcall(rb_cLazy, id_private, 1, sym(
"_enumerable_drop"));
4627 rb_funcall(rb_cLazy, id_private, 1, sym(
"_enumerable_drop_while"));
4628 rb_funcall(rb_cLazy, id_private, 1, sym(
"_enumerable_uniq"));
4629 rb_funcall(rb_cLazy, id_private, 1, sym(
"_enumerable_each_with_index"));
4662 lazy_use_super_method = rb_hash_new_with_size(19);
4663 rb_hash_aset(lazy_use_super_method, sym(
"map"), sym(
"_enumerable_map"));
4664 rb_hash_aset(lazy_use_super_method, sym(
"collect"), sym(
"_enumerable_collect"));
4665 rb_hash_aset(lazy_use_super_method, sym(
"flat_map"), sym(
"_enumerable_flat_map"));
4666 rb_hash_aset(lazy_use_super_method, sym(
"collect_concat"), sym(
"_enumerable_collect_concat"));
4667 rb_hash_aset(lazy_use_super_method, sym(
"select"), sym(
"_enumerable_select"));
4668 rb_hash_aset(lazy_use_super_method, sym(
"find_all"), sym(
"_enumerable_find_all"));
4669 rb_hash_aset(lazy_use_super_method, sym(
"filter"), sym(
"_enumerable_filter"));
4670 rb_hash_aset(lazy_use_super_method, sym(
"filter_map"), sym(
"_enumerable_filter_map"));
4671 rb_hash_aset(lazy_use_super_method, sym(
"reject"), sym(
"_enumerable_reject"));
4672 rb_hash_aset(lazy_use_super_method, sym(
"grep"), sym(
"_enumerable_grep"));
4673 rb_hash_aset(lazy_use_super_method, sym(
"grep_v"), sym(
"_enumerable_grep_v"));
4674 rb_hash_aset(lazy_use_super_method, sym(
"zip"), sym(
"_enumerable_zip"));
4675 rb_hash_aset(lazy_use_super_method, sym(
"take"), sym(
"_enumerable_take"));
4676 rb_hash_aset(lazy_use_super_method, sym(
"take_while"), sym(
"_enumerable_take_while"));
4677 rb_hash_aset(lazy_use_super_method, sym(
"drop"), sym(
"_enumerable_drop"));
4678 rb_hash_aset(lazy_use_super_method, sym(
"drop_while"), sym(
"_enumerable_drop_while"));
4679 rb_hash_aset(lazy_use_super_method, sym(
"uniq"), sym(
"_enumerable_uniq"));
4680 rb_hash_aset(lazy_use_super_method, sym(
"with_index"), sym(
"_enumerable_with_index"));
4681 rb_hash_aset(lazy_use_super_method, sym(
"each_with_index"), sym(
"_enumerable_each_with_index"));
4683 rb_vm_register_global_object(lazy_use_super_method);
4703 rb_define_method(rb_cGenerator,
"initialize_copy", generator_init_copy, 1);
4724 rb_define_method(rb_cEnumChain,
"initialize_copy", enum_chain_init_copy, 1);
4738 rb_define_method(rb_cEnumProduct,
"initialize", enum_product_initialize, -1);
4739 rb_define_method(rb_cEnumProduct,
"initialize_copy", enum_product_init_copy, 1);
4774Init_Enumerator(
void)
4776 id_rewind = rb_intern_const(
"rewind");
4777 id_next = rb_intern_const(
"next");
4778 id_result = rb_intern_const(
"result");
4779 id_receiver = rb_intern_const(
"receiver");
4780 id_arguments = rb_intern_const(
"arguments");
4781 id_memo = rb_intern_const(
"memo");
4782 id_method = rb_intern_const(
"method");
4783 id_force = rb_intern_const(
"force");
4784 id_to_enum = rb_intern_const(
"to_enum");
4785 id_each_entry = rb_intern_const(
"each_entry");
4786 sym_each =
ID2SYM(id_each);
4787 sym_yield =
ID2SYM(rb_intern_const(
"yield"));
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
#define rb_define_private_method(klass, mid, func, arity)
Defines klass#mid and makes it private.
void rb_include_module(VALUE klass, VALUE module)
Includes a module to a class.
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
void rb_need_block(void)
Declares that the current method needs a block.
void rb_undef_method(VALUE klass, const char *name)
Defines an undef of a method.
int rb_scan_args_kw(int kw_flag, int argc, const VALUE *argv, const char *fmt,...)
Identical to rb_scan_args(), except it also accepts kw_splat.
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Retrieves argument from argc and argv to given VALUE references according to the format string.
int rb_keyword_given_p(void)
Determines if the current method is given a keyword argument.
int rb_block_given_p(void)
Determines if the current method is given a block.
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
Keyword argument deconstructor.
#define T_COMPLEX
Old name of RUBY_T_COMPLEX.
#define RB_INTEGER_TYPE_P
Old name of rb_integer_type_p.
#define rb_str_buf_cat2
Old name of rb_usascii_str_new_cstr.
#define OBJ_INIT_COPY(obj, orig)
Old name of RB_OBJ_INIT_COPY.
#define RFLOAT_VALUE
Old name of rb_float_value.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define T_FLOAT
Old name of RUBY_T_FLOAT.
#define ID2SYM
Old name of RB_ID2SYM.
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
#define CLASS_OF
Old name of rb_class_of.
#define rb_ary_new4
Old name of rb_ary_new_from_values.
#define FIXABLE
Old name of RB_FIXABLE.
#define rb_exc_new2
Old name of rb_exc_new_cstr.
#define LONG2FIX
Old name of RB_INT2FIX.
#define T_RATIONAL
Old name of RUBY_T_RATIONAL.
#define T_HASH
Old name of RUBY_T_HASH.
#define NUM2DBL
Old name of rb_num2dbl.
#define rb_ary_new3
Old name of rb_ary_new_from_args.
#define LONG2NUM
Old name of RB_LONG2NUM.
#define Qtrue
Old name of RUBY_Qtrue.
#define ST2FIX
Old name of RB_ST2FIX.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define FIX2LONG
Old name of RB_FIX2LONG.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
#define NIL_P
Old name of RB_NIL_P.
#define ALLOCV_N
Old name of RB_ALLOCV_N.
#define POSFIXABLE
Old name of RB_POSFIXABLE.
#define T_SYMBOL
Old name of RUBY_T_SYMBOL.
#define DBL2NUM
Old name of rb_float_new.
#define NUM2LONG
Old name of RB_NUM2LONG.
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define rb_ary_new2
Old name of rb_ary_new_capa.
#define ALLOCV_END
Old name of RB_ALLOCV_END.
#define SYMBOL_P
Old name of RB_SYMBOL_P.
VALUE rb_rescue2(VALUE(*b_proc)(VALUE), VALUE data1, VALUE(*r_proc)(VALUE, VALUE), VALUE data2,...)
An equivalent of rescue clause.
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
void rb_iter_break(void)
Breaks from a block.
VALUE rb_eRangeError
RangeError exception.
VALUE rb_eTypeError
TypeError exception.
VALUE rb_eRuntimeError
RuntimeError exception.
VALUE rb_eStopIteration
StopIteration exception.
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
VALUE rb_exc_new_str(VALUE etype, VALUE str)
Identical to rb_exc_new_cstr(), except it takes a Ruby's string instead of C's.
VALUE rb_eIndexError
IndexError exception.
VALUE rb_mKernel
Kernel module.
VALUE rb_mEnumerable
Enumerable module.
VALUE rb_cEnumerator
Enumerator class.
VALUE rb_obj_hide(VALUE obj)
Make the object invisible from Ruby code.
VALUE rb_cNumeric
Numeric class.
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
VALUE rb_obj_dup(VALUE obj)
Duplicates the given object.
VALUE rb_inspect(VALUE obj)
Generates a human-readable textual representation of the given object.
VALUE rb_cRange
Range class.
VALUE rb_equal(VALUE lhs, VALUE rhs)
This function is an optimised version of calling #==.
VALUE rb_obj_is_kind_of(VALUE obj, VALUE klass)
Queries if the given object is an instance (of possibly descendants) of the given class.
VALUE rb_obj_freeze(VALUE obj)
Just calls rb_obj_freeze_inline() inside.
VALUE rb_to_int(VALUE val)
Identical to rb_check_to_int(), except it raises in case of conversion mismatch.
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
VALUE rb_funcall_with_block(VALUE recv, ID mid, int argc, const VALUE *argv, VALUE procval)
Identical to rb_funcallv_public(), except you can pass a block.
VALUE rb_call_super(int argc, const VALUE *argv)
This resembles ruby's super.
VALUE rb_ary_new_from_values(long n, const VALUE *elts)
Identical to rb_ary_new_from_args(), except how objects are passed.
VALUE rb_ary_dup(VALUE ary)
Duplicates an array.
VALUE rb_ary_cat(VALUE ary, const VALUE *train, long len)
Destructively appends multiple elements at the end of the array.
VALUE rb_check_array_type(VALUE obj)
Try converting an object to its array representation using its to_ary method, if any.
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_new_capa(long capa)
Identical to rb_ary_new(), except it additionally specifies how many rooms of objects it should alloc...
VALUE rb_ary_resize(VALUE ary, long len)
Expands or shrinks the passed array to the passed length.
VALUE rb_ary_hidden_new(long capa)
Allocates a hidden (no class) empty array.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
VALUE rb_ary_freeze(VALUE obj)
Freeze an array, preventing further modifications.
VALUE rb_ary_entry(VALUE ary, long off)
Queries an element of an array.
void rb_ary_store(VALUE ary, long key, VALUE val)
Destructively stores the passed value to the passed array's passed index.
#define RETURN_SIZED_ENUMERATOR(obj, argc, argv, size_fn)
This roughly resembles return enum_for(__callee__) unless block_given?.
VALUE rb_enumerator_size_func(VALUE recv, VALUE argv, VALUE eobj)
This is the type of functions that rb_enumeratorize_with_size() expects.
void rb_provide(const char *feature)
Declares that the given feature is already provided by someone else.
VALUE rb_num_coerce_cmp(VALUE lhs, VALUE rhs, ID op)
Identical to rb_num_coerce_bin(), except for return values.
VALUE rb_obj_method(VALUE recv, VALUE mid)
Creates a method object.
VALUE rb_block_proc(void)
Constructs a Proc object from implicitly passed components.
VALUE rb_proc_call_with_block(VALUE recv, int argc, const VALUE *argv, VALUE proc)
Identical to rb_proc_call(), except you can additionally pass another proc object,...
VALUE rb_proc_call_kw(VALUE recv, VALUE args, int kw_splat)
Identical to rb_proc_call(), except you can specify how to handle the last element of the given array...
VALUE rb_obj_is_proc(VALUE recv)
Queries if the given object is a proc.
int rb_range_values(VALUE range, VALUE *begp, VALUE *endp, int *exclp)
Deconstructs a range into its components.
#define rb_hash_uint(h, i)
Just another name of st_hash_uint.
#define rb_hash_end(h)
Just another name of st_hash_end.
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
VALUE rb_str_dup(VALUE str)
Duplicates a string.
VALUE rb_str_buf_append(VALUE dst, VALUE src)
Identical to rb_str_cat_cstr(), except it takes Ruby's string instead of C's.
void rb_str_set_len(VALUE str, long len)
Overwrites the length of the string.
st_index_t rb_hash_start(st_index_t i)
Starts a series of hashing.
VALUE rb_exec_recursive(VALUE(*f)(VALUE g, VALUE h, int r), VALUE g, VALUE h)
"Recursion" API entry point.
VALUE rb_ivar_set(VALUE obj, ID name, VALUE val)
Identical to rb_iv_set(), except it accepts the name as an ID instead of a C string.
VALUE rb_ivar_get(VALUE obj, ID name)
Identical to rb_iv_get(), except it accepts the name as an ID instead of a C string.
VALUE rb_class_path(VALUE mod)
Identical to rb_mod_name(), except it returns #<Class: ...> style inspection for anonymous modules.
int rb_respond_to(VALUE obj, ID mid)
Queries if the object responds to the method.
void rb_undef_alloc_func(VALUE klass)
Deletes the allocator function of a class.
VALUE rb_check_funcall(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcallv(), except it returns RUBY_Qundef instead of raising rb_eNoMethodError.
VALUE rb_check_funcall_kw(VALUE recv, ID mid, int argc, const VALUE *argv, int kw_splat)
Identical to rb_check_funcall(), except you can specify how to handle the last element of the given a...
void rb_define_alloc_func(VALUE klass, rb_alloc_func_t func)
Sets the allocator function of a class.
VALUE rb_sym2str(VALUE symbol)
Obtain a frozen string representation of a symbol (not including the leading colon).
VALUE rb_to_symbol(VALUE name)
Identical to rb_intern_str(), except it generates a dynamic symbol if necessary.
ID rb_to_id(VALUE str)
Identical to rb_intern_str(), except it tries to convert the parameter object to an instance of rb_cS...
char * ptr
Pointer to the underlying memory region, of at least capa bytes.
int len
Length of the buffer.
#define RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg)
Shim for block function parameters.
VALUE rb_yield_values(int n,...)
Identical to rb_yield(), except it takes variadic number of parameters and pass them to the block.
VALUE rb_yield_values2(int n, const VALUE *argv)
Identical to rb_yield_values(), except it takes the parameters as a C array instead of variadic argum...
VALUE rb_yield(VALUE val)
Yields the block.
VALUE rb_yield_values_kw(int n, const VALUE *argv, int kw_splat)
Identical to rb_yield_values2(), except you can specify how to handle the last element of the given a...
VALUE rb_block_call_func(RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg))
This is the type of a function that the interpreter expect for C-backended blocks.
VALUE rb_block_call_kw(VALUE obj, ID mid, int argc, const VALUE *argv, rb_block_call_func_t proc, VALUE data2, int kw_splat)
Identical to rb_funcallv_kw(), except it additionally passes a function as a block.
#define rb_long2int
Just another name of rb_long2int_inline.
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
VALUE rb_block_call(VALUE q, ID w, int e, const VALUE *r, type *t, VALUE y)
Call a method with a block.
VALUE rb_proc_new(type *q, VALUE w)
Creates a rb_cProc instance.
#define RARRAY_LEN
Just another name of rb_array_len.
#define RARRAY_AREF(a, i)
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
#define RHASH_EMPTY_P(h)
Checks if the hash is empty.
#define RUBY_TYPED_DEFAULT_FREE
This is a value you can set to rb_data_type_struct::dfree.
#define TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
struct rb_data_type_struct rb_data_type_t
This is the struct that holds necessary info for a struct.
#define TypedData_Make_Struct(klass, type, data_type, sval)
Identical to TypedData_Wrap_Struct, except it allocates a new data region internally instead of takin...
#define InitVM(ext)
This macro is for internal use.
#define RB_SCAN_ARGS_LAST_HASH_KEYWORDS
Treat a final argument as keywords if it is a hash, and not as keywords otherwise.
#define RB_PASS_CALLED_KEYWORDS
Pass keywords if current method is called with keywords, useful for argument delegation.
#define RB_NO_KEYWORDS
Do not pass keywords.
#define RTEST
This is an old name of RB_TEST.
#define _(args)
This was a transition path from K&R to ANSI.
Decomposed Enumerator::ArithmeicSequence.
int exclude_end
Whether the endpoint is open or closed.
VALUE end
"Right" or "highest" endpoint of the sequence.
VALUE step
Step between a sequence.
VALUE begin
"Left" or "lowest" endpoint of the sequence.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.