summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--mrblib/numeric.rb9
-rw-r--r--src/numeric.c17
2 files changed, 18 insertions, 8 deletions
diff --git a/mrblib/numeric.rb b/mrblib/numeric.rb
index dfdaf9da8..5a3c5eb58 100644
--- a/mrblib/numeric.rb
+++ b/mrblib/numeric.rb
@@ -104,14 +104,7 @@ module Integral
raise ArgumentError, "step can't be 0" if step == 0
return to_enum(:step, num, step) unless block
- i = self
- if Object.const_defined?(:Float) &&
- (kind_of?(Float) || num.kind_of?(Float) || step.kind_of?(Float))
- i = i.to_f
- num = num.to_f unless num == nil
- step = step.to_f
- end
-
+ i = __coerce_step_counter(num, step)
if num == nil
while true
block.call(i)
diff --git a/src/numeric.c b/src/numeric.c
index fc8460300..fa9daf8a7 100644
--- a/src/numeric.c
+++ b/src/numeric.c
@@ -154,6 +154,22 @@ num_div(mrb_state *mrb, mrb_value x)
#endif
}
+static mrb_value
+num_coerce_step_counter(mrb_state *mrb, mrb_value self)
+{
+ mrb_value counter = self, num, step;
+
+ mrb_get_args(mrb, "oo", &num, &step);
+
+#ifndef MRB_WITHOUT_FLOAT
+ if (mrb_float_p(self) || mrb_float_p(num) || mrb_float_p(step)) {
+ counter = mrb_funcall(mrb, counter, "to_f", 0);
+ }
+#endif
+
+ return counter;
+}
+
#ifndef MRB_WITHOUT_FLOAT
/********************************************************************
*
@@ -1542,6 +1558,7 @@ mrb_init_numeric(mrb_state *mrb)
mrb_define_method(mrb, numeric, ">=", num_ge, MRB_ARGS_REQ(1));
mrb_define_method(mrb, numeric, "finite?", num_finite_p, MRB_ARGS_NONE());
mrb_define_method(mrb, numeric, "infinite?",num_infinite_p, MRB_ARGS_NONE());
+ mrb_define_method(mrb, numeric, "__coerce_step_counter", num_coerce_step_counter, MRB_ARGS_REQ(2));
/* Integer Class */
integer = mrb_define_class(mrb, "Integer", numeric); /* 15.2.8 */