Break out of a loop

Is there an idiomatic(or more elegant way) of finding out whether I exited
a loop normally or because of an exceptional condition?

e.g

10.times{
# some processing

#if (some condition)
	break
end

#further processing

}

here i want to know if I came out because of the break or after looping

10 times.

Of course, one could set a flag to do the same, but I was wondering if
there was a ‘better’ way

regs
Vivek

···

A closed mouth gathers no feet

Vivek Nallur wrote:

Is there an idiomatic(or more elegant way) of finding out whether I exited
a loop normally or because of an exceptional condition?

In 1.7 you can do

x = loop do break 3 end
p x # ==> 3

Vivek Nallur wrote:

Is there an idiomatic(or more elegant way) of finding out whether I exited
a loop normally or because of an exceptional condition?

e.g

found = catch :found do

10.times{

some processing

#if (some condition)
throw :found, true
break
end

#further processing
false
}
end

here i want to know if I came out because of the break or after looping

10 times.
if found

This works in 1.6, too.

The key word is “exceptional”. Using exception handling is a good
option for what you want.

class ApplicationException < Exception; end

begin
10.times do
# some processing
raise ApplicationException if some.condition
# further processing
end
rescue ApplicationException
# error handling
end

Just in case you’re unfamiliar with exception handling in general, be
aware that you needn’t even handle it there. If the loop code was in
a method “foo”, you can do this:

begin
foo
rescue ApplicationException

end

The Pickaxe has all the details.

Regards,
Gavin

···

On Friday, January 31, 2003, 5:26:39 PM, Vivek wrote:

Is there an idiomatic(or more elegant way) of finding out whether I
exited a loop normally or because of an exceptional condition?

i = 0
catch(:oops) do
10.times { |i|
throw :oops if (some condition)
}
end
puts “Loop terminated prematurely” if i < 9
exit

···

On Fri, 31 Jan 2003 15:26:39 +0900, Vivek Nallur wrote:

Is there an idiomatic(or more elegant way) of finding out whether I exited
a loop normally or because of an exceptional condition?

e.g

10.times{

some processing

#if (some condition)
break
end

#further processing
}

here i want to know if I came out because of the break or after looping

10 times.

Of course, one could set a flag to do the same, but I was wondering if
there was a ‘better’ way

regs
Vivek

Hi,

Is there an idiomatic(or more elegant way) of finding out whether I exited
a loop normally or because of an exceptional condition?

e.g

10.times{

some processing

#if (some condition)
break
end

#further processing
}

In simplest case, `break’ed iterator returns nil, otherwise
what will be returned depends on its specification and
implementation. In this case, Integer#times returns self, so
you can write as:

finished = 10.times {
if some(condition)
break
end
}
if finished
puts “iterated #{finished} times”
else
puts “breaked”
end

···

At Fri, 31 Jan 2003 15:26:39 +0900, Vivek Nallur wrote:


Nobu Nakada

Thanks to both Joel and Gavin for their replies.

Ruby’s exception handling, though neat, is something that I’d rather do
for a bigger/structured/complicated program. For a script that’s hardly a
screenful, the throw-catch solution seems nice enough. Infact, now that
I’ve seen it, I seem to recall seeing something like this on rubygarden as
well(hindsight is 20/20 :slight_smile: ).

Thanks anyway, both of you.

regards
Vivek

···

A closed mouth gathers no feet

Or more:

result = catch(:oops) do
           10.times {|i| throw(:oops, :break) if some_condition}
         end
# result == :break if some condition

Namely throw can throw some value to a catch tag and catch returns the
thrown value.

So you also can break out on multiple conditions and know the cause,
or break nested loop and distinguish which loop breaks.

with no throw, result is a value of a block given to catch.

···

In message pan.2003.01.31.18.58.57.162192@nc.rr.com cyclists@nc.rr.com writes:

i = 0
catch(:oops) do
10.times { |i|
throw :oops if (some condition)
}
end
puts “Loop terminated prematurely” if i < 9
exit


kjana@dm4lab.to February 1, 2003
Of two evils choose the lesser.

Exactly what I wanted. Many thanks.

-Vivek

···

–>In simplest case, `break’ed iterator returns nil, otherwise
–>what will be returned depends on its specification and
–>implementation. In this case, Integer#times returns self, so
–>you can write as:
–>
–> finished = 10.times {
–> if some(condition)
–> break
–> end
–> }
–> if finished
–> puts “iterated #{finished} times”
–> else
–> puts “breaked”
–> end
–>
–>

The only true love is love at first sight; second sight dispels it.