From: Sam Roberts Date: 2004-03-19T03:21:16+09:00 Subject: 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? $ ruby every.rb 3 6 9 12 15 18 21 24 is 20 in every? nil is 21 in every? 21 all n between 100 and 200? nil all n as a string, up to 20 nil --> I would like not to have gotten nil for the last two... $ cat every.rb class EveryN include Enumerable def initialize(n) @n = n end def each y = 0 loop do y = y + @n yield y end end end every = EveryN.new(3) every.each { |n| break if n > 25; puts n } puts "is 20 in every?" p every.detect { |n| break if n > 20; n == 20 } puts "is 21 in every?" p every.detect { |n| break if n > 21; n == 21 } puts "all n between 100 and 200?" p every.find_all { |n| break if n > 200; n >= 100 && n <= 200 } puts "all n as a string, up to 20" p every.collect { |n| break if n > 20; "n is #{n}" } -- Sam Roberts