Skip to content

Commit 1a68f1b

Browse files
committed
Extract checks for the block in lazy enumerator
1 parent 32977f3 commit 1a68f1b

File tree

1 file changed

+13
-31
lines changed

1 file changed

+13
-31
lines changed

enumerator.c

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1602,6 +1602,11 @@ lazy_init_block_i(RB_BLOCK_CALL_FUNC_ARGLIST(val, m))
16021602
#define LAZY_MEMO_SET_PACKED(memo) ((memo)->memo_flags |= LAZY_MEMO_PACKED)
16031603
#define LAZY_MEMO_RESET_PACKED(memo) ((memo)->memo_flags &= ~LAZY_MEMO_PACKED)
16041604

1605+
#define LAZY_NEED_BLOCK(func) \
1606+
if (!rb_block_given_p()) { \
1607+
rb_raise(rb_eArgError, "tried to call lazy " #func " without a block"); \
1608+
}
1609+
16051610
static VALUE lazy_yielder_result(struct MEMO *result, VALUE yielder, VALUE procs_array, VALUE memos, long i);
16061611

16071612
static VALUE
@@ -1805,9 +1810,7 @@ lazy_initialize(int argc, VALUE *argv, VALUE self)
18051810
VALUE generator;
18061811

18071812
rb_check_arity(argc, 1, 2);
1808-
if (!rb_block_given_p()) {
1809-
rb_raise(rb_eArgError, "tried to call lazy new without a block");
1810-
}
1813+
LAZY_NEED_BLOCK(new);
18111814
obj = argv[0];
18121815
if (argc > 1) {
18131816
size = argv[1];
@@ -2065,10 +2068,7 @@ static const lazyenum_funcs lazy_map_funcs = {
20652068
static VALUE
20662069
lazy_map(VALUE obj)
20672070
{
2068-
if (!rb_block_given_p()) {
2069-
rb_raise(rb_eArgError, "tried to call lazy map without a block");
2070-
}
2071-
2071+
LAZY_NEED_BLOCK(map);
20722072
return lazy_add_method(obj, 0, 0, Qnil, Qnil, &lazy_map_funcs);
20732073
}
20742074

@@ -2150,10 +2150,7 @@ static const lazyenum_funcs lazy_flat_map_funcs = {
21502150
static VALUE
21512151
lazy_flat_map(VALUE obj)
21522152
{
2153-
if (!rb_block_given_p()) {
2154-
rb_raise(rb_eArgError, "tried to call lazy flat_map without a block");
2155-
}
2156-
2153+
LAZY_NEED_BLOCK(flat_map);
21572154
return lazy_add_method(obj, 0, 0, Qnil, Qnil, &lazy_flat_map_funcs);
21582155
}
21592156

@@ -2180,10 +2177,7 @@ static const lazyenum_funcs lazy_select_funcs = {
21802177
static VALUE
21812178
lazy_select(VALUE obj)
21822179
{
2183-
if (!rb_block_given_p()) {
2184-
rb_raise(rb_eArgError, "tried to call lazy select without a block");
2185-
}
2186-
2180+
LAZY_NEED_BLOCK(select);
21872181
return lazy_add_method(obj, 0, 0, Qnil, Qnil, &lazy_select_funcs);
21882182
}
21892183

@@ -2214,10 +2208,7 @@ static const lazyenum_funcs lazy_filter_map_funcs = {
22142208
static VALUE
22152209
lazy_filter_map(VALUE obj)
22162210
{
2217-
if (!rb_block_given_p()) {
2218-
rb_raise(rb_eArgError, "tried to call lazy filter_map without a block");
2219-
}
2220-
2211+
LAZY_NEED_BLOCK(filter_map);
22212212
return lazy_add_method(obj, 0, 0, Qnil, Qnil, &lazy_filter_map_funcs);
22222213
}
22232214

@@ -2243,10 +2234,7 @@ static const lazyenum_funcs lazy_reject_funcs = {
22432234
static VALUE
22442235
lazy_reject(VALUE obj)
22452236
{
2246-
if (!rb_block_given_p()) {
2247-
rb_raise(rb_eArgError, "tried to call lazy reject without a block");
2248-
}
2249-
2237+
LAZY_NEED_BLOCK(reject);
22502238
return lazy_add_method(obj, 0, 0, Qnil, Qnil, &lazy_reject_funcs);
22512239
}
22522240

@@ -2529,10 +2517,7 @@ static const lazyenum_funcs lazy_take_while_funcs = {
25292517
static VALUE
25302518
lazy_take_while(VALUE obj)
25312519
{
2532-
if (!rb_block_given_p()) {
2533-
rb_raise(rb_eArgError, "tried to call lazy take_while without a block");
2534-
}
2535-
2520+
LAZY_NEED_BLOCK(take_while);
25362521
return lazy_add_method(obj, 0, 0, Qnil, Qnil, &lazy_take_while_funcs);
25372522
}
25382523

@@ -2627,10 +2612,7 @@ static const lazyenum_funcs lazy_drop_while_funcs = {
26272612
static VALUE
26282613
lazy_drop_while(VALUE obj)
26292614
{
2630-
if (!rb_block_given_p()) {
2631-
rb_raise(rb_eArgError, "tried to call lazy drop_while without a block");
2632-
}
2633-
2615+
LAZY_NEED_BLOCK(drop_while);
26342616
return lazy_add_method(obj, 0, 0, Qfalse, Qnil, &lazy_drop_while_funcs);
26352617
}
26362618

0 commit comments

Comments
 (0)