I’ve got the feeling that this thread is
drifting from the original problem (detect
successful looping) to the discussion about
some related but different problems.
But now I miss the global view of a
loop construction syntax handling the
different passways of a loop.
We never claimed that there is no way
to do this or that with Ruby.
(See below).
The subject is about a principle loop
construct as syntactic sugar for
easily describe the different passes?
Problem 1: Identify bypassing (e.g. empty array)
and declare coding that shall run in this case
Problem 2: Identify interruption (by break or throw)
and declare coding that shall run in this case
Problem 3: Identify full passing (no break)
and declare coding that shall run in this case.
David Alan Black schrieb:
if array.empty?
puts “Empty array”
else
array.each {|e| puts e}
end
Ok, this solves problem 1 in some way (although it
does not ‘identify’ the bypassing itself, but checks
explicitly a precondition). So in relation to loops
you would check a condition twice:
if a > b
while a > b
…
end
else
…
end
The catch/throw solves problem 3 and, yes, problem 2
in some kind (suggestion of Marcin Kowalczyk):
j = 3 # or j = 11 or greater
catch (:done) do
for i in 1…10
if i == j
print “Interruption branch\n”
throw :done
end
print “Working in loop \n”
end
print “Loop terminated without break\n”
end
Although it is a matter of design decision if
the full code of the break-branch should be
written at this point ('print "Interruption...')
or at a specific place external to this algorithm.
Well, this could be done in a special text block of
a defined syntax construct ( an 'on_break' or whatever)
or in by calling a function/method at this point.
So the complete example looks something like:
if array.empty?
print “Loop bypassed \n”
else
catch (:done) do
for i in array
if #
print “Interruption branch\n”
throw :done
end
print “Working in loop \n”
end
print “Loop terminated without break\n”
end
end
Now I have the different cases of my loop termination
on different places with different indentations ...
The primary goal, expressed with "Working in loop" is
somewhat hidden between the control stuff and all.
Hum, the discussed construct with different
for-branches would simplify things … don’t you
think too?
Bye
Det