From: David Alan Black Date: 2002-07-10T01:52:38+09:00 Subject: Re: for ... else ... end Hello -- On Tue, 9 Jul 2002, Austin Ziegler wrote: > cnt=0 > anArray.each do |i| > break if i<0 > res=19*i+4-i > puts res > cnt+=res > else > puts "Empty array" > on_exit > puts "The result is #{cnt}" > on_break # or on_abort > # ok, I admit it is like an exception... > puts "Invalid negative element found in the array!" > end I still don't see how a code block can have an 'else' clause. Wouldn't this make for some awfully bizarre code? array.each {|e| else puts "Empty array"} Given an empty array, this else clause would execute. It seems to me to be crossing all sorts of borders and boundaries for a proc/block to have this kind of knowledge. Is a block of code really supposed to know whether or not it was executed, without any explicit test? And, come to think of it.... shouldn't the block not be entered at all if the array is empty? What the "else" thing means is that the block *will* be entered. I wish I knew the technical term for why that isn't OK :-) but doesn't it feel very non-OK? I guess this thread is bringing out my Ruby-conservative streak :-) I'm personally very happy to do something like this (which I know isn't identical to your example, but still): if arr.empty? puts "Empty array" elsif arr.detect {|x| x < 0} puts "Illegal negative number!" # or raise and catch TypeError else total = arr.inject(0) do |n,m| tmp = n + (19*m+4-m) puts tmp tmp end puts "The result is #{total}" end (requires 1.7.x, whichever one #inject was added to :-) David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav