Peña, Botp
12/14/2007 3:22:00 AM
From: Bernardo Monteiro Rufino [mailto:bermonruf@gmail.com]=20
# class Integer
# def mytimes(start=3D0, inc=3D1, &block)
# start.step(start + (self * inc) - 1, inc, &block);
ah, indeed; and as always, any refactoring/change must pass the test..=20
irb(main):011:0> 0.mytimes(-1,-1){|x| p x}
-1
-2
=3D> -1
irb(main):012:0> 1.mytimes(-1,-1){|x| p x}
-1
-2
-3
=3D> -1
irb(main):013:0> 2.mytimes(-1,-1){|x| p x}
-1
-2
-3
-4
=3D> -1
irb(main):014:0> 2.mytimes(-1,0){|x| p x}
ArgumentError: step can't be 0
from (irb):3:in `step'
from (irb):3:in `mytimes'
from (irb):14
from :0
btw, i do not like the way ruby arranges it's params for #step. I prefer =
the step/inc arg to be the first arg since it reads clearly. eg,
instead of 1.step 10, 2
i like 1.step 2, 10
reads clearly, and the step/inc is emphasize.
kind regards -botp