T
timr
This code works because 'exit' within the lambda within the block
stops the loop. However, I don't want to stop the program. I need to
use break rather than exit. But break won't work within that lambda--
the loop doesn't stop. I can of course reorganize the statement so
that it isn't a terse one-liner, and get it to work. But I would like
to be able to break the loop from within that lambda function if it is
possible. Somehow, I guess I need to make that break have a binding
to the method. Any ideas?!?
def solve_by_iter
counter = 1
loop do
yield counter
counter += 1
end
end
solve_by_iter { |test| lambda{puts test; exit}.call if (1..6).all?{|
num| test%num == (num-1)} }
#I want to use: solve_by_iter { |test| lambda{puts test; break}.call
if (1..6).all?{|num| test%num == (num-1)} }
stops the loop. However, I don't want to stop the program. I need to
use break rather than exit. But break won't work within that lambda--
the loop doesn't stop. I can of course reorganize the statement so
that it isn't a terse one-liner, and get it to work. But I would like
to be able to break the loop from within that lambda function if it is
possible. Somehow, I guess I need to make that break have a binding
to the method. Any ideas?!?
def solve_by_iter
counter = 1
loop do
yield counter
counter += 1
end
end
solve_by_iter { |test| lambda{puts test; exit}.call if (1..6).all?{|
num| test%num == (num-1)} }
#I want to use: solve_by_iter { |test| lambda{puts test; break}.call
if (1..6).all?{|num| test%num == (num-1)} }