I want to increase my step value in each loop iteration, but my solution is not working.
n=1
(0..100).step(n) do |x|
puts x
n+=1
end
Is there any way to change "n" or I'm must using "while loop" or smth else?
I assuming you are trying to print 1, 3, 6, 10, 15, 21 etc.
step documentation says:
Iterates over range, passing each nth element to the block. If the range contains numbers, n is added for each iteration.
So what you are trying to do can't be done with step
. A while
or traditional for
loop should do the trick.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With