[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: next, retry, break?

MenTaLguY

12/10/2005 7:41:00 PM

On Sat, 2005-12-10 at 14:34 +0900, Yukihiro Matsumoto wrote:
> | retry: unwinds the stack to the call
> | site of the "closest" yielding
> | method, calling it again with
> | the same arguments
> |
> | break: unwinds the stack to the call
> | site of the "closest" yielding
> | method, using the given value
> | (or nil) as the method's result
> |
> |Is that right?
>
> Right, if the term "closest" means what I thought.

I think the word I wanted was "innermost"..?

> |If so, what if I want retry or break to unwind further (e.g. because I
> |am implementing one iterator in terms of another)?
>
> I'm not sure what you want. "retry" and "break" jumps out of the
> method immediately, so that you can do nothing. But you can use
> "ensure" if you really need iteration termination process.

I just tried this and it works fine:

class Spleen
def initialize
@arr = [1, 2, 3]
end
def each(&blk)
@arr.each(&blk)
self
end
end

s = Spleen.new
k = s.each { break 42 }
p k #=> 42

I'm happy now, except (given my understanding of 'break' behavior above)
I don't understand why it works. Does the call to Array#each with &blk
count as the innermost yield? Or does it cause the yield in Array#each
to be accounted to Spleen#each?

Otherwise, it seems like it is unwinding to the method call to which the
block is originally attached, rather than only to the innermost yielding
one.

Thank you for your patience,

-mental
2 Answers

Yukihiro Matsumoto

12/11/2005 12:03:00 AM

0

Hi,

In message "Re: next, retry, break?"
on Sun, 11 Dec 2005 04:40:33 +0900, MenTaLguY <mental@rydia.net> writes:

|> Right, if the term "closest" means what I thought.
|
|I think the word I wanted was "innermost"..?

Then, I think you were wrong.

|I just tried this and it works fine:
|
| class Spleen
| def initialize
| @arr = [1, 2, 3]
| end
| def each(&blk)
| @arr.each(&blk)
| self
| end
| end
|
| s = Spleen.new
| k = s.each { break 42 }
| p k #=> 42
|
|I'm happy now, except (given my understanding of 'break' behavior above)
|I don't understand why it works. Does the call to Array#each with &blk
|count as the innermost yield? Or does it cause the yield in Array#each
|to be accounted to Spleen#each?

"break" breaks the lexically closest one, in this case Spleen.each,
not Array#each called within Spleen#s.

matz.


MenTaLguY

12/11/2005 1:49:00 AM

0

On Sun, 2005-12-11 at 09:03 +0900, Yukihiro Matsumoto wrote:

> "break" breaks the lexically closest one, in this case Spleen.each,
> not Array#each called within Spleen#s.

Thank you! That was the root of my confusion. I was under the mistaken
impression that the "target" of break/retry was determined dynamically
rather than lexically.

Thanks again for your patience,

-mental