summaryrefslogtreecommitdiff
path: root/range.c
diff options
context:
space:
mode:
authormrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-08-06 09:08:28 +0000
committermrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-08-06 09:08:28 +0000
commitf15069338debcaab151b589de9bcc32acffa6ca0 (patch)
treec315767e47c948fc9404d27beff43ff06b2a5199 /range.c
parent1777e39c2a78c969d7e86af78e381c8d00df9772 (diff)
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
Diffstat (limited to 'range.c')
-rw-r--r--range.c18
1 files changed, 13 insertions, 5 deletions
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 {