From: timr Date: 2010-08-28T13:40:08+09:00 Subject: Re: how about Array#collect_until > I would rather place this code in Enumerable.  Then all other classes > mixing in this module will benefit. > Good idea. > >    def collect_until(fn,&block) > >      results = [] > >      self.each do |x| > > Why does everybody write "self.each" when "each" is sufficient? self.sorry, er, I mean sorry. No you're right. It is overly explicit. > > >        result = (yield x) > >        results<<  result > >        break if fn.call(result) > > As far as I can see this is not in line with your original requirement > because in your original posting you evaluated the termination condition > on the original value while this code does it on the result of the > mapping calculation. From the original posting: [1,2,3].collect_until({|x| x%2 == 0}) {|item| item + 10} # => [11,12] To get this to work the result from the yield to {|item| item + 10} block would be run through the {|x| x%2 == 0} block to check if we are done. So, I was shooting for a method that would send to block, take the result and check it against fn and quit if condition was met. With the call made as [].collect_until(fn,&block). I am trying to save processing from sending any more items than are required to a calculation intensive block. The other tests may help clarify the expected behavior. In each case the method should result in a collection that DOES NOT INCLUDE A RESULT FOR EVERY ITEM in the original set since the condition is met part way through and break is executed. Therefore, it provides the most a collection of calculations upto and including the solution when an end condition was met. > > p %w[ find out if that  this  but not the other mother > > mouth].collect_until(lambda {|word| word[-2,2] == 'st' }){|item| item > > + "t" } > > > puts "another attempt" > > > p [1,6,7,5,3,0,10,12,14].collect_until(Proc.new {|num| num>  25 }){| > > item| item * 4 } > > > puts "another attempt" > > p [1,2,3].collect_until(Proc.new {|x| x%2 == 0}) {|item| item + 10} > > Kind regards > >         robert > > -- > remember.guy do |as, often| as.you_can - without endhttp://blog.rubybestpractices.com/