开发者

each_with_index desired column output Rails

开发者 https://www.devze.com 2023-01-21 16:29 出处:网络
<% source.strains.each_with_index do |strain, j| %> <% if (j == (source.strains.size - 1)) %>
<% 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, th开发者_StackOverflow中文版en i need to print first value of loop (j[0]). Kindly suggest me or correct above script.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号