Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change step value inside the loop?

Tags:

loops

ruby

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?

like image 790
shade88 Avatar asked Dec 09 '22 06:12

shade88


1 Answers

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.

like image 111
Benjamin Udink ten Cate Avatar answered Jan 04 '23 03:01

Benjamin Udink ten Cate