numeric.rb (step): iterate over integers even if end is a float

It used to check all `start`, `end` and `step`. If either of them are
float number, `#step` iterated over float number. Now we don't check the
type of `end` argument.
This commit is contained in:
Yukihiro "Matz" Matsumoto
2024-08-03 13:35:09 +09:00
parent 6d6e26661d
commit e8bb03da40
3 changed files with 6 additions and 10 deletions
+1 -1
View File
@@ -104,7 +104,7 @@ class Integer
raise ArgumentError, "step can't be 0" if step == 0
return to_enum(:step, num, step) unless block
i = __coerce_step_counter(num, step)
i = __coerce_step_counter(step)
if num == self || step.infinite?
block.call(i) if step > 0 && i <= (num||i) || step < 0 && i >= (num||-i)
elsif num == nil
+4 -8
View File
@@ -266,17 +266,13 @@ int_quo(mrb_state *mrb, mrb_value x)
static mrb_value
coerce_step_counter(mrb_state *mrb, mrb_value self)
{
mrb_value num, step;
mrb_get_args(mrb, "oo", &num, &step);
#ifndef MRB_NO_FLOAT
mrb->c->ci->mid = 0;
if (mrb_float_p(num) || mrb_float_p(step)) {
mrb_value step = mrb_get_arg1(mrb);
#ifndef MRB_NO_FLOAT
if (mrb_float_p(step)) {
return mrb_ensure_float_type(mrb, self);
}
#endif
return self;
}
@@ -2169,7 +2165,7 @@ mrb_init_numeric(mrb_state *mrb)
mrb_define_method_id(mrb, integer, MRB_SYM(to_s), int_to_s, MRB_ARGS_OPT(1)); /* 15.2.8.3.25 */
mrb_define_method_id(mrb, integer, MRB_SYM(inspect), int_to_s, MRB_ARGS_OPT(1));
mrb_define_method_id(mrb, integer, MRB_SYM(divmod), int_divmod, MRB_ARGS_REQ(1)); /* 15.2.8.3.30(x) */
mrb_define_method_id(mrb, integer, MRB_SYM(__coerce_step_counter), coerce_step_counter, MRB_ARGS_REQ(2));
mrb_define_method_id(mrb, integer, MRB_SYM(__coerce_step_counter), coerce_step_counter, MRB_ARGS_REQ(1));
/* Fixnum Class for compatibility */
mrb_define_const_id(mrb, mrb->object_class, MRB_SYM(Fixnum), mrb_obj_value(integer));
+1 -1
View File
@@ -66,7 +66,7 @@ assert('Numeric#step') do
skip unless Object.const_defined?(:Float)
inf = Float::INFINITY
assert_raise(ArgumentError) { 1.step(2, 0.0) { break } }
assert_step([2.0, 3.0, 4.0], 2, [4.0])
assert_step([2, 3, 4], 2, [4.0])
assert_step([7.0, 4.0, 1.0, -2.0], 7, [-4, -3.0])
assert_step([2.0, 3.0, 4.0], 2.0, [4])
assert_step([10.0, 11.0, 12.0, 13.0], 10.0, [], inf: true)