summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTOMITA Masahiro <[email protected]>2014-11-26 01:34:07 +0900
committerTOMITA Masahiro <[email protected]>2014-11-26 01:34:07 +0900
commitbbab89e730eeb74009887b0975f17539e24c4ccd (patch)
tree08047b32c9a4c3d9677b51a4e06e430b4a4589a8
parent1365151dcb65190b05a97768584f68cb14d584c4 (diff)
downloadmruby-bbab89e730eeb74009887b0975f17539e24c4ccd.tar.gz
mruby-bbab89e730eeb74009887b0975f17539e24c4ccd.zip
Fix: Numeric#step infinite loop.
-rw-r--r--mrblib/numeric.rb14
1 files changed, 11 insertions, 3 deletions
diff --git a/mrblib/numeric.rb b/mrblib/numeric.rb
index 5be3c90fc..1f44a2c81 100644
--- a/mrblib/numeric.rb
+++ b/mrblib/numeric.rb
@@ -101,12 +101,20 @@ module Integral
# incremented by +step+ (default 1).
#
def step(num, step=1, &block)
+ raise ArgumentError, "step can't be 0" if step == 0
return to_enum(:step, num, step) unless block_given?
i = if num.kind_of? Float then self.to_f else self end
- while(i <= num)
- block.call(i)
- i += step
+ if step > 0
+ while(i <= num)
+ block.call(i)
+ i += step
+ end
+ else
+ while(i >= num)
+ block.call(i)
+ i += step
+ end
end
self
end