From: paron Date: 2009-06-12T20:45:21+09:00 Subject: Re: Idiomatic way to detect first/last iteration? > I just want to know if there is a clean, idiomatic way, > specifically in ruby (not rails library code) to do something similar, > detecting the first/last iteration of a collection. Is detection really what you want? Doesn't that imply that you test each iteration? Since the array is ordered by definition, it seems (needs tested!) that creating three entities might be faster. in helper: def first_also_ran_last(records) first_record=records.shift last_record=records.pop return [first_record, records, last_record] end in view: <% parsed_records = first_also_ran_last(@whatever) <%="Really like #{parsed_records[0]}"%> <% parsed_records[1].each do |also_ran|%> #(if any) <%= "Sorta like #{also_ran}"%> <%end -%> <%unless parsed_records[2].nil?-%> <%= "Don't much like #{parsed_records[2]}"%> <%end-> Don't know if it's faster, or more idiomatic, but perhaps it's more semantic? Ron