Start a loop from 1 Start a loop from 1 ruby ruby

Start a loop from 1


Ruby supports a number of ways of counting and looping:

1.upto(10) do |i|  puts iend>> 1.upto(10) do |i| >     puts i|    end #=> 112345678910

There's also step instead of upto which allows you to increment by a step value:

>> 1.step(10,2) { |i| puts i } #=> 113579


You could use a range:

(1..10).each { |i| puts i }

Ranges give you full control over the starting and ending indexes (as long as you want to go from a lower value to a higher value).


Try

(1..10).each do |i| #  ... i goes from 1 to 10end

instead. It is also easier to read when the value of i matters.