From 2b188ed8a191257f23ddf6f8a27bf1d3964587ed Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Wed, 26 Aug 2020 17:53:04 +0900 Subject: Reorganize `Integer` system. - Integrate `Fixnum` and `Integer` - Remove `Integral` - `int / int -> int` - Replace `mrb_fixnum()` to `mrb_int()` - Replace `mrb_fixnum_value()` to `mrb_int_value()`. - Use `mrb_integer_p()` instead of `mrb_fixnum_p()` --- mrblib/numeric.rb | 47 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 36 insertions(+), 11 deletions(-) (limited to 'mrblib') diff --git a/mrblib/numeric.rb b/mrblib/numeric.rb index 5926518d5..e28d63324 100644 --- a/mrblib/numeric.rb +++ b/mrblib/numeric.rb @@ -34,11 +34,11 @@ class Numeric end ## -# Integral +# Integer # -# mruby special - module to share methods between Floats and Integers -# to make them compatible -module Integral +# ISO 15.2.8 +## +class Integer ## # Calls the given block once for each Integer # from +self+ downto +num+. @@ -125,14 +125,7 @@ module Integral end self end -end -## -# Integer -# -# ISO 15.2.8 -class Integer - include Integral ## # Returns the receiver simply. # @@ -161,3 +154,35 @@ class Integer # ISO 15.2.8.3.26 alias truncate floor end + +class Float + ## + # Calls the given block from +self+ to +num+ + # incremented by +step+ (default 1). + # + def step(num=nil, step=1, &block) + raise ArgumentError, "step can't be 0" if step == 0 + return to_enum(:step, num, step) unless block + + i = self + if num == self || step.infinite? + block.call(i) if step > 0 && i <= (num||i) || step < 0 && i >= (num||-i) + elsif num == nil + while true + block.call(i) + i += step + end + elsif step > 0 + while i <= num + block.call(i) + i += step + end + else + while i >= num + block.call(i) + i += step + end + end + self + end +end -- cgit v1.2.3