DID YOU KNOW? -- break can return a value?

T

trans. (T. Onoma)

An interesting tidbit I pickup today. Did you know that break can return a
value from it's block?

a = loop do
break (1 + 1)
end

puts a #=> 4


T.
--
( o _ カラãƒ
// trans.
/ \ (e-mail address removed)

I don't give a damn for a man that can only spell a word one way.
-Mark Twain
 
D

Dave Burt

trans. (T. Onoma) said:
An interesting tidbit I pickup today. Did you know that break can return a
value from it's block?

a = loop do
break (1 + 1)
end

puts a #=> 4

What platform gives that counterintuitive result? *grins*
 
Z

zuzu

What platform gives that counterintuitive result? *grins*

by defining the variable "a" with code "loop", it is a Proc object, yes?

(sorry, i would test this myself except i'm away from my ruby console.)

(if so, i must simultaneously pronounce my dislike for imperative loop
structures, and my praise for ruby perceiving all code as in blocks /
procedures (functional/subroutine style).)

also, quite right dave, what gives with the "trailing off the edge of
a procedure 'return' " producing a "value" of 4? perhaps that's the
point; what exactly is going on when passing arguments into the break
function/keyword?

peace,
-z
 
F

Florian Frank

also, quite right dave, what gives with the "trailing off the edge of
a procedure 'return' " producing a "value" of 4? perhaps that's the
point; what exactly is going on when passing arguments into the break
function/keyword?

Whatever it may be, I am quite sure 1 + 1 is still equal to 2.
 
T

trans. (T. Onoma)

39:40 +0900, zuzu wrote:
| > also, quite right dave, what gives with the "trailing off the edge of
| > a procedure 'return' " producing a "value" of 4? perhaps that's the
| > point; what exactly is going on when passing arguments into the break
| > function/keyword?
|
| Whatever it may be, I am quite sure 1 + 1 is still equal to 2.

:) Yes, that's a typo, a would be 2, not 4. Strange how easy that is to
overlook!

T.
 

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,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top