From: Adam Keys Date: 2005-04-22T22:34:33+09:00 Subject: Re: for .. in .. else? Salutations, On Apr 22, 2005, at 7:17 AM, David A. Black wrote: > 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!
Maybe I missed something, but when I extracted this code and ran it in irb, it didn't work like I thought it would: irb(main):087:0> songs => ["Rocky Raccoon", "As My Guitar Gently Weeps", "She Came In Through The Bathroom Window"] irb(main):088:0> unless songs.each do |song| irb(main):089:2* p song irb(main):090:2> end.empty? irb(main):091:1> p 'no songs!' irb(main):092:1> end "Rocky Raccoon" "As My Guitar Gently Weeps" "She Came In Through The Bathroom Window" "no songs!" => nil irb(main):093:0> The presence of 'no songs!' is not what I'd expected. > > 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) This makes sense in isolation, however given the above result I'm curious as to what is actually going on. As to the temporary variable, I am more tired of having "foo = []" all over my code. > 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 :-) I agree the {} block looks more like something that yields a value than do..end. I think I had it that way at first but the line spilled over past column 80 so I changed it to a do...end, which I use when a block spans multiple lines. Is that a common idiom or am I unique in this regard? -- ~akk http://therealadam.com