From f15069338debcaab151b589de9bcc32acffa6ca0 Mon Sep 17 00:00:00 2001 From: mrkn Date: Mon, 6 Aug 2018 09:08:28 +0000 Subject: enumerator.c: Introduce Enumerator::ArithmeticSequence This commit introduces new core class Enumerator::ArithmeticSequence. Enumerator::ArithmeticSequence is a subclass of Enumerator, and represents a number generator of an arithmetic sequence. After this commit, Numeric#step and Range#step without blocks returned an ArithmeticSequence object instead of an Enumerator. This class introduces the following incompatibilities: - You can create a zero-step ArithmeticSequence, and its size is not ArgumentError, but Infinity. - You can create a negative-step ArithmeticSequence from a range. [ruby-core:82816] [Feature #13904] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64205 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- range.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'range.c') diff --git a/range.c b/range.c index bcc8d1a8f4..56c1ee253b 100644 --- a/range.c +++ b/range.c @@ -388,18 +388,26 @@ range_step(int argc, VALUE *argv, VALUE range) { VALUE b, e, step, tmp; - RETURN_SIZED_ENUMERATOR(range, argc, argv, range_step_size); - b = RANGE_BEG(range); e = RANGE_END(range); if (argc == 0) { - step = INT2FIX(1); + step = INT2FIX(1); } else { - rb_scan_args(argc, argv, "01", &step); - step = check_step_domain(step); + rb_scan_args(argc, argv, "01", &step); + } + + if (!rb_block_given_p()) { + if (rb_obj_is_kind_of(b, rb_cNumeric) && (NIL_P(e) || rb_obj_is_kind_of(e, rb_cNumeric))) { + return rb_arith_seq_new(range, ID2SYM(rb_frame_this_func()), argc, argv, + range_step_size, b, e, step, EXCL(range)); + } + + RETURN_SIZED_ENUMERATOR(range, argc, argv, range_step_size); } + step = check_step_domain(step); + if (FIXNUM_P(b) && NIL_P(e) && FIXNUM_P(step)) { long i = FIX2LONG(b), unit = FIX2LONG(step); do { -- cgit v1.2.3