In your typical each loop in Rails, how do I determine the last object, because I want to do something different to it than the rest of the objects.
<% @stuff.each do |thing| %> <% end %>
you can do a count(). or if you're looking for ONLY the last element, you can use end(). end(arr); returns only the last element.
Use Counter in PHP foreach Loop Use count() to determine the total length of an array. The iteration of the counter was placed at the bottom of the foreach() loop - $x++; to execute the condition to get the first item. To get the last item, check if the $x is equal to the total length of the array.
@stuff.each do |s| ...normal stuff... if s == @stuff.last ...special stuff... end end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With