From: Robert Klemme Date: 2006-07-06T23:55:26+09:00 Subject: Re: A newbie would like some code criticism, please... Just Another Victim of the Ambient Morality wrote: > I'm learning Ruby and I'd like some criticism on a program that I wrote. > It interfaces with iTunes and runs through all the songs, (reasonably) > ensuring proper title capitalization. It's a small program and I would have > simply posted except that, for some unknown reason, MS Outlook Express keeps > converting the tabs into single spaces. > I'm open to comments on style versus what is standard protocol for the > Ruby community, as well. > I hope the program may be as useful to any of you as it is for me... > > http://theorem.ca/~dlkong/iTunes_title_filter.rb.gz > > > 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? Sometimes also #inject will help but that depends on what you do in the block. For example: irb(main):001:0> %w{aa bb cc dd}.inject {|a, b| a + ", " + b} => "aa, bb, cc, dd" Other than that I'd probably stick with the solution that uses #each_with_index because it at least avoids creating a new array. Kind regards robert