From: Daniel Schierbeck Date: 2006-07-06T23:53:47+09:00 Subject: Re: A newbie would like some code criticism, please... Just Another Victim of the Ambient Morality wrote: > first = true > list.each do |item| > if first > # do something special for the first case > > first = false > else > # do something else for every other item > end > end > > Is there a better way to do this? Perhaps some way to simply iterate > over every element other than the first one? Something that avoid a check > every iteration? There are several ways of doing this: list.each_with_index do |item, i| if i == 0 # first item else # not the first item end end Though it's not very pretty # returns the first item and removes it # from the array list.shift list.each do |item| # the rest of the items end Note that this alters `list'! The following won't: # returns the first item, keeps it in # the array list.first ary[1...ary.length].each do |item| # all but the first item end I'm sure there are even more ways of doing it. Cheers, Daniel