summaryrefslogtreecommitdiffhomepage
path: root/mrblib/numeric.rb
diff options
context:
space:
mode:
Diffstat (limited to 'mrblib/numeric.rb')
-rw-r--r--mrblib/numeric.rb47
1 files changed, 36 insertions, 11 deletions
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