每个_with_index所需的列输出Rails

发布于 2024-09-28 10:19:37 字数 339 浏览 1 评论 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 %>

我需要获得输出,

如果 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怼怹恏 2024-10-05 10:19:37

相同,

<font size="3">Strain <%= source.strains.last[0].appendix %> </font>

看起来您的代码与(没有任何循环)

请查看 Array#last

但即使您不知道 last 方法,创建一个循环来访问集合中的最后一个元素也有点奇怪。您至少可以执行collection[collection.size - 1]

评论
那你为什么要使用 strain[0] 而不是 source.strains[0] 呢? source.strains 是您的集合,strain 只是循环中的当前元素。我认为 strain 也是某种数组。

Looks like your code is the same as

<font size="3">Strain <%= source.strains.last[0].appendix %> </font>

(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, do collection[collection.size - 1].

on comment
Then why're you doing strain[0] instead of source.strains[0]? source.strains is your collection and strain is just a current element in the loop. I thought strain is some kind of array too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文