From: Jon Evans Date: 2006-07-06T23:54:01+09:00 Subject: Re: A newbie would like some code criticism, please... Hi, > There's also a pattern that often comes up for me and I would like some > help with it... > > 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? If you don't mind clobbering 'list': do_something_for_first(list.shift) list.each do |item| do_something_else(item) end or if you can't alter list, how about: do_something_for_first(list[0]) list[1..-1].each do |item| ... When faced with problems like that I find it handy to fire up irb and just try different things until the solution emerges. It also helps to have the pages for 'Enumerable' and 'Array' bookmarked in Programming Ruby. Jon