diff --git a/numeric.c b/numeric.c index e3a8b87671..49d27b82a8 100644 --- a/numeric.c +++ b/numeric.c @@ -1859,39 +1859,44 @@ ruby_num_interval_step_size(VALUE from, VALUE to, VALUE step, int excl) } } -#define NUM_STEP_SCAN_ARGS(argc, argv, to, step, hash, desc) do { \ - argc = rb_scan_args(argc, argv, "02:", &to, &step, &hash); \ - if (!NIL_P(hash)) { \ - step = rb_hash_aref(hash, sym_by); \ - to = rb_hash_aref(hash, sym_to); \ - } \ - else { \ - /* compatibility */ \ - if (argc > 1 && NIL_P(step)) { \ - rb_raise(rb_eTypeError, "step must be numeric"); \ - } \ - if (rb_equal(step, INT2FIX(0))) { \ - rb_raise(rb_eArgError, "step can't be 0"); \ - } \ - } \ - if (NIL_P(step)) { \ - step = INT2FIX(1); \ - } \ - desc = !positive_int_p(step); \ - if (NIL_P(to)) { \ - to = desc ? DBL2NUM(-INFINITY) : DBL2NUM(INFINITY); \ - } \ -} while (0) +static int +num_step_scan_args(int argc, const VALUE *argv, VALUE *to, VALUE *step) +{ + VALUE hash; + int desc; + + argc = rb_scan_args(argc, argv, "02:", to, step, &hash); + if (!NIL_P(hash)) { + step = rb_hash_aref(hash, sym_by); + to = rb_hash_aref(hash, sym_to); + } + else { + /* compatibility */ + if (argc > 1 && NIL_P(*step)) { + rb_raise(rb_eTypeError, "step must be numeric"); + } + if (rb_equal(*step, INT2FIX(0))) { + rb_raise(rb_eArgError, "step can't be 0"); + } + } + if (NIL_P(*step)) { + *step = INT2FIX(1); + } + desc = !positive_int_p(*step); + if (NIL_P(*to)) { + *to = desc ? DBL2NUM(-INFINITY) : DBL2NUM(INFINITY); + } + return desc; +} static VALUE num_step_size(VALUE from, VALUE args, VALUE eobj) { - VALUE to, step, hash; - int desc; + VALUE to, step; int argc = args ? RARRAY_LENINT(args) : 0; const VALUE *argv = args ? RARRAY_CONST_PTR(args) : 0; - NUM_STEP_SCAN_ARGS(argc, argv, to, step, hash, desc); + num_step_scan_args(argc, argv, &to, &step); return ruby_num_interval_step_size(from, to, step, FALSE); } @@ -1952,12 +1957,12 @@ num_step_size(VALUE from, VALUE args, VALUE eobj) static VALUE num_step(int argc, VALUE *argv, VALUE from) { - VALUE to, step, hash; + VALUE to, step; int desc, inf; RETURN_SIZED_ENUMERATOR(from, argc, argv, num_step_size); - NUM_STEP_SCAN_ARGS(argc, argv, to, step, hash, desc); + desc = num_step_scan_args(argc, argv, &to, &step); if (RTEST(rb_num_coerce_cmp(step, INT2FIX(0), id_eq))) { inf = 1; }