我的视图中有一个变量"x“。我需要显示一些代码"x“的次数。
我基本上想像这样设置一个循环:
for i = 1 to x
do something on (i)
end
有没有办法做到这一点?
发布于 2011-03-21 02:24:53
如果您在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
发布于 2011-03-21 02:21:08
x.times do |i|
something(i+1)
end
发布于 2014-03-30 02:40:24
for i in 0..max
puts "Value of local variable is #{i}"
end
https://stackoverflow.com/questions/5370436
复制相似问题