最佳答案
我总是使用一个计数器来检查循环中的第一项(i==0
) :
i = 0
my_array.each do |item|
if i==0
# do something with the first item
end
# common stuff
i += 1
end
有没有一种更优雅的方法来做到这一点(也许是一种方法) ?