From: "David A. Black" Date: 2005-04-22T21:17:50+09:00 Subject: Re: for .. in .. else? Hi -- On Fri, 22 Apr 2005, Adam Keys wrote: > On Apr 21, 2005, at 5:46 PM, Hal Fulton wrote: >> FWIW I'll offer this (untested) alternative: >> >> >> >> <% unless song_list.each do |song|%> >> >> <% end.empty? %> >> >> <% end %> >> >>
Songs
<%= song.name %>
no songs!
> > A few weeks ago I was on a crusade to not create temporary Arrays and Hashes. > I wrote a bit of code something like this: > > ary.reject do |item| > item.some_predicate? > end.each do |doodad| > doodad.do_something > end > > I was showing it to a non-Rubyist and he thought it was a little hard to > read. I agreed it may be a little too clever for its own good. Obviously > Hal doesn't think so :). Does anyone else have strong feelings on code like > the above? Yours is different from Hal's, because in Hal's the iteration through the song list is sort of a side-effect of the testing of the condition, whereas you're just doing a standard chaining thing. And you do create a temporary array :-) (the return value of #reject) You could perhaps make it easier to read with a different formatting: ary.reject {|e| e.some_predicate? } .each do |f| f.do_something end or something. (Definitely lose the one-space indenting.) If your friend still has trouble with it, buy him the Pickaxe :-) David -- David A. Black dblack@wobblini.net