Very useful... 'break' accepts an argument

Greetings,

I just discovered undocumented but very useful syntax:

  break <expression>

The expression seems to add a useful feature to a 'while', 'until' or 'for' control structure. For example:

  result = while <condition>
  ...
    break <something>
  ...
  end

'result' is nil if the loop exits with <condition> and is <something> if the break is executed.

Very useful and provides an cool addition to an already elegant collection of iterator constructs!

Tom Agnew3

Tom Agnew wrote:

Greetings,

I just discovered undocumented but very useful syntax:

    break <expression>

The expression seems to add a useful feature to a 'while', 'until' or
'for' control structure. For example:

    result = while <condition>
    ...
      break <something>
    ...
    end

'result' is nil if the loop exits with <condition> and is <something> if
the break is executed.

Very useful and provides an cool addition to an already elegant
collection of iterator constructs!

Tom Agnew3
    
It's also cool because it works with your own iterators (really, any
method that takes a block), not just ruby syntax like while.

def foo
  yield
end

x = foo do
  break 4
end

p x

···

--
      vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Unfortunately the base iterators return the object they are iterating over
instead of nil like the non-iterator loops (while, until, loop). For the
non-iterator loops nil can mean terminate normally if you use break with a
value (non-nil). For iterator loops you have to do the opposite - break with
a value is of little use.

I discussed the problem here:

http://rcrchive.net/rcr/show/305

···

On 10/30/05, Joel VanderWerf <vjoel@path.berkeley.edu> wrote:

Tom Agnew wrote:
> Greetings,
>
> I just discovered undocumented but very useful syntax:
>
> break <expression>
>
> The expression seems to add a useful feature to a 'while', 'until' or
> 'for' control structure. For example:
>
> result = while <condition>
> ...
> break <something>
> ...
> end
>
> 'result' is nil if the loop exits with <condition> and is <something> if
> the break is executed.
>
> Very useful and provides an cool addition to an already elegant
> collection of iterator constructs!
>
> Tom Agnew3
>

It's also cool because it works with your own iterators (really, any
method that takes a block), not just ruby syntax like while.

def foo
yield
end

x = foo do
break 4
end

p x

valid point. i use the following pattern for those situations:

   config =
     catch('config') do
       search_path.each |dir|
         candidate = File::join dir, "#{ $0 }.conf"
         throw 'config', candidate if test ?e, candidate
       end
       nil
     end

   unless config
     warn{ "using default config" }
     config = default_config
   end

it would be nice to do this more compactly.

cheers.

-a

···

On Mon, 31 Oct 2005, Eric Mahurin wrote:

Unfortunately the base iterators return the object they are iterating over
instead of nil like the non-iterator loops (while, until, loop). For the
non-iterator loops nil can mean terminate normally if you use break with a
value (non-nil). For iterator loops you have to do the opposite - break with
a value is of little use.

I discussed the problem here:

RCR 305: return nil from simple object loops

--

email :: ara [dot] t [dot] howard [at] noaa [dot] gov
phone :: 303.497.6469
anything that contradicts experience and logic should be abandoned.
-- h.h. the 14th dalai lama

===============================================================================