Very useful... 'break' accepts an argument

T

Tom Agnew

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
 
J

Joel VanderWerf

Tom said:
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
 
E

Eric Mahurin

------=_Part_13706_2851730.1130718888581
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

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 =3D foo do
break 4
end

p x

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 wit=
h
a value is of little use.

I discussed the problem here:

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

------=_Part_13706_2851730.1130718888581--
 
A

Ara.T.Howard

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

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
--
===============================================================================
| 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
===============================================================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top