How to create an integer-for-loop in Ruby?
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
x.times do |i|
something(i+1)
end
You can perform a simple each
loop on the range from 1 to `x´:
(1..x).each do |i|
#...
end
for i in 0..max
puts "Value of local variable is #{i}"
end
All Ruby loops