gpt4 book ai didi

ruby - 如何在 Ruby 中创建整数循环?

转载 作者:数据小太阳 更新时间:2023-10-29 06:19:54 26 4
gpt4 key购买 nike

我的 View 中有一个变量“x”。我需要显示一些代码“x”次。

我基本上想像这样设置一个循环:

for i = 1 to x
do something on (i)
end

有办法吗?

最佳答案

如果您在 erb View (对于 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

关于ruby - 如何在 Ruby 中创建整数循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5370436/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com