I want to do something different with the last loop iteration when performing \'foreach\' on an object. I\'m using Ruby but the same goes for C#, Java etc.
Would it be a viable solution for your case to just take the first/last elements out of your array before doing the "general" each run?
Like this:
list = ['A','B','C','D'] first = list.shift last = list.pop puts "First one: #{first}" list.each{|i| puts "Looping: "+i } puts "Last one: #{last}"