Ruby - Using while and until loops as for loop

Introduction

You can use while and until loops to iterate over arrays and other collections.

For example, the following code shows two ways of iterating over all the elements in an array:

Demo

arr= [1,2,3,4,5] 
i = 0 # ww  w  . j a  v  a  2s  . c o  m

while i < arr.length 
    puts(arr[i]) 
    i += 1 
end 
i=0 
until i == arr.length 
    puts(arr[i]) 
    i +=1 
end

Result

Related Topic