Sam Roberts wrote:
Similar problem with detect, etc., once the times yielded are out of
range, I’m not interested anymore, and want to break.What to do?
You can use throw & catch:
class A
def each
item = firstItem
items =
catch (:done) do
loop do
throw :done if reachedLimit(item)
items << item
yield item
item = item.succ
end
end
items
end
include Enumerable
def firstItem; 0; end
def reachedLimit(item); item > 10; end
end
a = A.new
p a.include?(3)
p a.include?(11)
p a.each { |i| puts i }