How to create an integer-for-loop in Ruby?

Solution 1:

If you're doing this in your erb view (for Rails), be mindful of the <% and <%= differences. What you'd want is:

<% (1..x).each do |i| %>
  Code to display using <%= stuff %> that you want to display    
<% end %>

For plain Ruby, you can refer to: http://www.tutorialspoint.com/ruby/ruby_loops.htm

Solution 2:

x.times do |i|
    something(i+1)
end

Solution 3:

for i in 0..max
   puts "Value of local variable is #{i}"
end

All Ruby loops

Solution 4:

You can perform a simple each loop on the range from 1 to `x´:

(1..x).each do |i|
  #...
end