From: walter@... Date: 2004-03-19T03:37:09+09:00 Subject: Re: Can I stop each's iteration, and get the results so far? > Basically, I want to break from a find_all, and get the array of > elements found so far. Is this possible? > > I tried this, no luck with find_all and collect, they return nil, > instead of the array so far. Any suggestions on how to do this? This might be overly simplistic, but if this is not going to be used by multiple threads simultaneously, and you already know the limit ahead of time, why not just set the limit on the object before calling the function, and make sure that each limits you to that? class EveryN include Enumerable def initialize(n) @n = n end def each y = 0 loop do y = y + @n break if @max_iteration < y yield y end end def max_iteration=(max_iteration) @max_iteration = max_iteration end end every = EveryN.new(3) every.max_iteration = 25 every.each { |n| puts n } puts "is 20 in every?" every.max_iteration = 20 p every.detect { |n| n == 20 } puts "is 21 in every?" every.max_iteration = 21 p every.detect { |n| n == 21 } puts "all n between 100 and 200?" every.max_iteration = 200 p every.find_all { |n| n >= 100} puts "all n as a string, up to 20" every.max_iteration = 20 p every.collect { |n| "n is #{n}" } Walt ***************************************************** Walter Szewelanczyk IS Director M.W. Sewall & CO. email : walter@mwsewall.com 259 Front St. Phone : (207) 442-7994 x 128 Bath, ME 04530 Fax : (207) 443-6284 *****************************************************