每个_with_index所需的列输出Rails
<% source.strains.each_with_index do |strain, j| %>
<% if (j == (source.strains.size - 1)) %>
<font size="3">Strain <%= strain[0].appendix %> </font>
<% end %>
<% end %>
我需要获得输出,
如果 j 值是循环的最后一个,那么我需要打印循环的第一个值 (j[0])。请建议我或更正上面的脚本。
<% source.strains.each_with_index do |strain, j| %>
<% if (j == (source.strains.size - 1)) %>
<font size="3">Strain <%= strain[0].appendix %> </font>
<% end %>
<% end %>
I need to get output as
If j value is last one of a loop, then i need to print first value of loop (j[0]). Kindly suggest me or correct above script.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
相同,
看起来您的代码与(没有任何循环)
请查看 Array#last
但即使您不知道
last
方法,创建一个循环来访问集合中的最后一个元素也有点奇怪。您至少可以执行collection[collection.size - 1]
。评论
那你为什么要使用
strain[0]
而不是source.strains[0]
呢?source.strains
是您的集合,strain
只是循环中的当前元素。我认为strain
也是某种数组。Looks like your code is the same as
(Without any loop)
Check out Array#last
But even if you didn't know about
last
method, making a loop to access last element in collection is kinda weird. You can, at least, docollection[collection.size - 1]
.on comment
Then why're you doing
strain[0]
instead ofsource.strains[0]
?source.strains
is your collection andstrain
is just a current element in the loop. I thoughtstrain
is some kind of array too.