my_array.each_with_index do |item, index| if index == 0 # do something with the first item end end
Since this question was asked, I'm sure somebody would want to do something similar on an Active Record object. I Googled but couldn't find anything relevant. So, how do you find out if you're on the first item of a loop/each on an Active Record object?
Since it's an object and not an array, you can't access the index in the same way as with an array. What you could do is the following:
@users = User.all @users.each do |user| first_item = (user.id == @users.id) ? true : false end