(Epiloque: argh Do not have time to reedit this mail now,
so take it as my loud thoughts).
Sean Chittenden schrieb:
for elt in arr
puts elt
else
puts "no rows"
ensure
puts "done looping"
end
Ahm, this seems a slightly different problem.
Here the else marks the case that the loop
never has been entered, not interrupted by ‘break’.
And what do you suggest with ‘ensure’ (“done looping”)
here?
(hum, took a look at the Exception description in pickaxe
for comparision):
f = File.open(“testfile”)
begin
… process
rescue
… handle error
else
puts "Congratulations-- no errors!"
ensure
f.close unless f.nil?
end
Here the else is described as:
The body of an else clause is executed only if no
exceptions are raised by the main body of code.
<<<<<
replacing ‘exceptions’ with ‘breaks’ of a for-loop
context would argue for ‘else’ to work like in Python.
Then there would be consistency between ‘else’ in 'begin’
and ‘else’ in a ‘for’ loop.
OTOH: What matches better to natural understanding?
The ‘else’ in a ‘begin’ context is naturally bound to
’rescue’. So you say:
BEGIN a process, do RESCUE for any exceptions ELSE
do something else.
But you naturally would not expect a for loop to break
but to pass. Therefore saying:
Do a process FOR every item in a list ELSE do something
(Python interpretation of ‘else’ ) seems confusing.
Defining a for loop as non-passing in normal situations
would cause the following sematic:
FOR every item in list do a process.
if INTERRUPTED do something ELSE do some other thing.
But this looks very similar to the Exception context.
Saying:
FOR every item in list do a process
when PASSED do something ELSE do some other thing.
causes the converse semantic: a for loop normally passes.
But in both cases the ‘else’ relates to the hook used before
(either INTERRUPTED or PASSED) and not directly to the FOR.
This correlates with the Exception semantic, where the 'else’
refers to the ‘rescue’ and not to the ‘begin’.
Regarding the ‘ensure’ …
I think I did not understand if it is a necessary
part or only declarative sugar, as IMHO every
code after the end is run in either case.
So writing:
f = File.open(“testfile”)
begin
# … process
[…]
ensure
f.close
end
or writing:
f = File.open(“testfile”)
begin
# … process
[…]
end
f.close
should be identical (?)
Bye
Det