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