/ Published in: Ruby
URL: http://www.ruby-doc.org/core/classes/Enumerable.html#M003168
It's great that, at the times I really need it, I can ask for Ruby help on #nyc.rb:
<capn_bluebeard> how can I detect the first or last iteration of a loop in
Ruby or Rails?
<fhwang> what sort of loop? an array loop?
<fhwang> or something else?
<harrisj> f == array.first?
<fhwang> in case of an array you want #each_with_index
<capn_bluebeard> yah each_with_index would do it, thanks much
<capn_bluebeard> I'm looping through an array printing LIs and I want to apply
css classes to the first and last LIs respectively
Expand |
Embed | Plain Text
- @random_pubs[0..3].each_with_index do |pub, count| %li{:class => (count > 0 ? (count == 3 ? 'last' : '') : 'first')} .description= pub.desc
You need to login to post a comment.
