我有一个变量"x".我需要多次显示代码"x".
我基本上想建立一个这样的循环:
for i = 1 to x
do something on (i)
end
有办法做到这一点吗?
我有一个变量"x".我需要多次显示代码"x".
我基本上想建立一个这样的循环:
for i = 1 to x
do something on (i)
end
有办法做到这一点吗?
如果您是在erb视图(对于Rails)中这样做的,请注意<%
和<%=
的区别.你想要的是:
<% (1..x).each do |i| %>
Code to display using <%= stuff %> that you want to display
<% end %>
对于纯Ruby,您可以参考:http://www.tutorialspoint.com/ruby/ruby_loops.htm