From: Austin Ziegler Date: 2003-03-31T14:04:07+09:00 Subject: Re: How to allow iteration over a collection that can be enumerated in multiple ways? On Mon, 31 Mar 2003 13:34:58 +0900, Sam Roberts wrote: > I thought of a 3rd way, but I don't know if its too clever. > > Basically, I can write one method, that takes a cond block as its > argument, and returns a class which defines "each" in terms of > that condition, and includes enumerable. Now I only have to write > two methods, and I can let Enumerable do the heavy lifting for me. > Or is that too clever/confusing? >> def each_by_group(group) ... for all matches yield(match) end >> def array_of_group(group) Array.new(all matches...) end I personally would look at it from an SQL-like perspective. Enumerable includes a method #find_all or #select which pretty much does what you want from array_of_group, I think. list.select { |obj| select-condition }.each { |e| puts e } # => objects matching select-condition have If you want to try to combine the methods, I think your writing it as "each_by_group" (I would call it "group_each" or "each_group") is probably the better method. -austin -- Austin Ziegler, austin@halostatue.ca on 2003.03.30 at 23:56:50