Ruby - Using loop control variable

Introduction

All of the iterators automatically pass the state of the iteration to the looped code as a parameter.

You can then retrieve into a variable and use, like so:

Demo

1.upto(5) { |number| puts number }

Result

For example, this code works in exactly the same way as that in the previous example:

Demo

1.upto(5) do |number|  
 puts number 
end

Result

Related Example