On Mon, 24 Nov 2003, T. Onoma wrote:

> This WILL brake code. Consider:
>   a = [1,2,3]
>   a.each { |x| i = x + i.to_i }
>   p "#{i}"  # => "6"
>   a = [1,2,3]
>   a.each { |x| i = x + i.to_i }
>   p "#{i}"  # => "12"  # => Doh! It's not 6 anymore!

The current policy on the value of i is that i doesn't exist in the
outerscope, so currently it doesn't matter that its value is not 6,
because the variable isn't available at all so the first p i doesn't do
anything.

#<NameError: (eval):1: undefined local variable or method `i'>



I think the actual problem would be more with code that does things like
this:

def foo(n)
  (0...n).map {
    proc {a=0; proc{a+=1;a}}[]
  }
end

I expect this code to produce n distinct counters that will each produce
their own independent sequence 1,2,3,4,5,... when called repeatedly. If it
becomes impossible to do this anymore, then I won't be able to honestly
say that Ruby really supports closures.

________________________________________________________________
Mathieu Bouchard                       http://artengine.ca/matju