Yield

Hello
in which context is the code of yield executed?
How do I get the code location (in which file/module/class/method the code
is written in, and in which contents it is executed ?

Example:

def ff k=0; begin k+=1; yield; end
           until k>50;
end
z=Array.new; k=0; z[0]=10;
ff {z[k+1]=z[k]+1}; p z
# shouldnt be used the same k in ff because already defined outside??
why does z have only 2 elements???

Thanks Berg

the block passed to `ff` in your last line is bound to the variables of it's surrounding context (it's a closure).

The method ff however, can not see the outside `k`, since methods aren't closures.

You have only 2 elements in z, because the outside `k` is never incremented.

···

On 02/27/2016 04:45 PM, A Berger wrote:

Hello
in which context is the code of yield executed?
How do I get the code location (in which file/module/class/method the code is written in, and in which contents it is executed ?

Example:

def ff k=0; begin k+=1; yield; end
           until k>50;
end
z=Array.new; k=0; z[0]=10;
ff {z[k+1]=z[k]+1}; p z
# shouldnt be used the same k in ff because already defined outside??
why does z have only 2 elements???

Thanks Berg

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;