hello,

f("two") creates a lambda that sees 'x' as it was at the time of its 
creation. zero that is.

the 'x' that f("one") uses is in a different closure.

konstantin

"Sam Kong" <sam.s.kong / gmail.com> wrote in message 
news:1141155782.675003.313420 / e56g2000cwe.googlegroups.com...
> Hello!
>
> Since my last question about
> closure(http://groups.google.com/group/comp.lang.ruby/browse_frm/thread/0d7e258107e1683e/12e182a6ff493232?hl=en#12e182a6ff493232)
> I am testing my understanding making some examples.
>
> Here's some code:
>
> def f name
> x = 0
>
> case name
> when "one"
> lambda {x += 1; puts x}
> when "two"
> lambda {puts x}
> end
> end
>
> one = f "one"
> two = f "two"
>
> one.call
> two.call
> one.call
> two.call
>
> Result:
>
> 1
> 0
> 2
> 0
>
>
> I expected the following.
> 1
> 1
> 2
> 2
>
>
> I thought that x is shared between one and two but they don't seem to.
> Or am I missing something?
>
> Thanks.
>
> Sam
>